Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2011, 02:44 PM   #1 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Exclamation Accessing UIButton from another class not working?

I am trying to access my UIButton in my Play class from my CCLayerClass.

The problem is that it is not working!

Here is how I declare it in the Play class: .h
Code:
IBOutlet UIButton *pauseButton;
BOOL pauseButtonVisible;

@property(nonatomic, retain) IBOutlet UIButton *pauseButton;
@property(readwrite) BOOL pauseButtonVisible;
.m

Code:
@synthesize pauseButton;

- (void)setPauseButtonVisible: (BOOL) variableToSet {
    pauseButtonVisible = variableToSet;
    if(pauseButton)
        [pauseButton setHidden: !pauseButtonVisible];
}
- (BOOL) pauseButtonVisible
{
    return(pauseButtonVisible);
}
viewWillAppear:

Code:
[pauseButton setHidden: !pauseButtonVisible];
I also made sure I connected it in Interface Builder

Then in CCLayerClass I do this:

Code:
Play *play = [[[Play alloc] init] autorelease];
    if(play.pauseButton == NULL) {  
        NSLog( @"pause button is NULL");
    }
play.pauseButtonVisible = YES;
But that NSLog gets called! Why is my pauseButton NULL? I just need to alloc it so it stays alive, is that possible?

Thanks!
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 03:46 PM   #2 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Hmm I just thought of this, if I alloced the button like this:
play.pauseButton = UIButton alloc] etc...... Maybe that would fix it. I am sure that I am just doing something stupid here :P
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 04:06 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Objective Zero View Post
I am trying to access my UIButton in my Play class from my CCLayerClass.

The problem is that it is not working!

Here is how I declare it in the Play class: .h
Code:
IBOutlet UIButton *pauseButton;
BOOL pauseButtonVisible;

@property(nonatomic, retain) IBOutlet UIButton *pauseButton;
@property(readwrite) BOOL pauseButtonVisible;
.m

Code:
@synthesize pauseButton;

- (void)setPauseButtonVisible: (BOOL) variableToSet {
    pauseButtonVisible = variableToSet;
    if(pauseButton)
        [pauseButton setHidden: !pauseButtonVisible];
}
- (BOOL) pauseButtonVisible
{
    return(pauseButtonVisible);
}
viewWillAppear:

Code:
[pauseButton setHidden: !pauseButtonVisible];
I also made sure I connected it in Interface Builder

Then in CCLayerClass I do this:

Code:
Play *play = [[[Play alloc] init] autorelease];
    if(play.pauseButton == NULL) {  
        NSLog( @"pause button is NULL");
    }
play.pauseButtonVisible = YES;
But that NSLog gets called! Why is my pauseButton NULL? I just need to alloc it so it stays alive, is that possible?

Thanks!

You should never muck with the view objects of a view controller outside of that view controller. That breaks the MVC design pattern, and tightly couples the two view controllers together.

A view controller's views are the private property of that view controller. If view controllers start playing with each other's privates, the are effectively married to each other, and you can't change one without changing the other.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 04:11 PM   #4 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

In this case I have to otherwise what I want done will never be possible (Im using Cocos2D also). All I simply want to do is unhide a button at a particular instance but it is NULL.

So how could I fix the button from being NULL? Also I can't call the ViewDidLoad in the other View Controller if you were going to mention a fix otherwise it will mess up the features in my app!

Edit: How about this. If you say I shouldn't control a object from another view controller that the object isn't it, is it possible to call a method in the view controller that does hold the object but without calling the ViewDidLoad?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8

Last edited by Objective Zero; 11-21-2011 at 04:50 PM.
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 05:05 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

This function doesn't make any sense:

- (BOOL) pauseButtonVisible
{
return(pauseButtonVisible);
}


also you need to either use interface manager to allocate the button or allocate it in viewDidLoad. Once its allocated you should be able to pass it into any function you want and manipulate it however u like.
RickSDK is offline   Reply With Quote
Old 11-21-2011, 05:15 PM   #6 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

But I want to avoid calling the ViewDidLoad. Maybe there is a workaround for that too which I am unaware of. I guess I can call it but how would I set a BOOL from my CCLayerClass and then use that BOOL value in the Play class's ViewDidLoad to either do the normal ViewDidLoad stuff or to do nothing?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 05:25 PM   #7 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

its not clear what you are trying to do, but whatever is it, you should be able to allocate it in viewDidLoad, then pass it to whatever function you want, which can then pass back a bool. You can then use that bool to modify your button however you want.

If you do your coding in phases, you might have better luck. When getting ready to write code that spans more than one script, i usually do something like this:

Step 1) Get the entire code to work in viewDidLoad of a single script.
Step 2) create a seperate function within the same script that passes info back and forth.
Step 3) Move that function out to the other script where you wanted it in the first place.

not sure if that makes sense, but good luck
RickSDK is offline   Reply With Quote
Old 11-21-2011, 05:37 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Objective Zero View Post
In this case I have to otherwise what I want done will never be possible (Im using Cocos2D also). All I simply want to do is unhide a button at a particular instance but it is NULL.

So how could I fix the button from being NULL? Also I can't call the ViewDidLoad in the other View Controller if you were going to mention a fix otherwise it will mess up the features in my app!

Edit: How about this. If you say I shouldn't control a object from another view controller that the object isn't it, is it possible to call a method in the view controller that does hold the object but without calling the ViewDidLoad?
Indeed, add a boolean hideButton property to the view controller.

In your view controller's viewWillAppear method, add code to show/hide the button based on the property.

If another class will ever set the hideButton property while the view controller is visible, create a custom setter that sets the button's hidden property after changing the instance variable.

That way, you can set the property at any time, even if the view controller's views haven't been loaded yet.

That is almost certainly why the button is nil when you're trying to set it (and another reason not do change views from outside the view controller.)

Another reason not to change views from outside the view controller is low memory. In a low memory condition, the system will unload the views from every view controller except the frontmost view controller. If you have the state data for your view objects (button show/hide state, label text if it changes, slider values, etc) saved in instance variables, and have the viewWillAppear method set the values, the view controller recreates it's views and puts them back the way they were supposed to be automatically.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 06:04 PM   #9 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Ok I will do so, but the only part I am confused about is this:
Quote:
If another class will ever set the hideButton property while the view controller is visible, create a custom setter that sets the button's hidden property after changing the instance variable.
What do you mean by that?

And after I do all of this code how would I call my Play view from my CCLayerClass?
Play *class = [[[Play alloc] init] autorelease];
class.buttonHidden = NO;

?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 06:11 PM   #10 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

@synthesize automatically creates 2 methods, a setter and getter. You need to override the setter to not only set the ivar but to change the button's hidden property.

Code:
- (void)setButtonHidden:(BOOL)newButtonHiddenValue {
//assign newButtonHiddenValue to buttonHidden
//assign buttonHidden to the button's hidden property
}
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 11-21-2011, 06:57 PM   #11 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Ok so here is my whole code for Play (correct me if anything is wrong!)
.h
Code:
IBOutlet UIButton *pauseButton;
    BOOL hideButton;
@property(nonatomic, retain) IBOutlet UIButton *pauseButton;
.m
Code:
@synthesize pauseButton;

- (void)setHideButton:(BOOL)myHiddenProperty {
    hideButton = myHiddenProperty;
    pauseButton.hidden = hideButton;
}
viewwillappear:
Code:
[pauseButton setHidden:hideButton];
Anything wrong here?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8

Last edited by Objective Zero; 11-21-2011 at 08:00 PM. Reason: Edited code
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 07:40 PM   #12 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

I think I explained it wrong, you've got the assignments backwards.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 11-21-2011, 08:00 PM   #13 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Check my edited code, is that good?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 08:14 PM   #14 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Objective Zero View Post
Ok so here is my whole code for Play (correct me if anything is wrong!)
.h
Code:
IBOutlet UIButton *pauseButton;
    BOOL hideButton;
@property(nonatomic ) IBOutlet UIButton *pauseButton;
.m
Code:
@synthesize pauseButton;

- (void)setHideButton:(BOOL)myHiddenProperty {
    hideButton = myHiddenProperty;
    pauseButton.hidden = hideButton;
}
viewwillappear:
Code:
[pauseButton setHidden:hideButton];
Anything wrong here?

Try this:

Code:
{
  IBOutlet UIButton *pauseButton;
  BOOL hideButton;
}
@property(nonatomic) BOOL hideButton;
.m
Code:
@synthesize hideButton;

//This is a custom setter for the hideButton property.
- (void)setHideButton:(BOOL)newValue 
{
    //set the instance variable to the new value
    hideButton = newValue;

    //also show/hide the button based on the new value. 
    //If the view hasn't been loaded, this won't do anything.
    pauseButton.hidden = hideButton;
}
Code:
- (void)viewWillAppear:(BOOL)animated
{
  //Update the button state in case it was changed 
  //before the view was loaded
  pauseButton.hidden = hideButton;
}


A class outside this view controller will just need to say

Code:
theViewController.hideButton = TRUE;
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 08:23 PM   #15 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Ok so just to be sure I call it like this correct?
Code:
Play *play = [[[Play alloc] init] autorelease];
    play.hideButton = NO;
That will unhide it. But currently I want the button to be hidden at first so I clicked the hidden property in the viewdidload but it still shows at launch. So I want it to be hidden at launch but then unhidden from the CCLayerClass in one of the methods.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-21-2011, 08:41 PM   #16 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Objective Zero View Post
Ok so just to be sure I call it like this correct?
Code:
Play *play = [[[Play alloc] init] autorelease];
    play.hideButton = NO;
That will unhide it. But currently I want the button to be hidden at first so I clicked the hidden property in the viewdidload but it still shows at launch. So I want it to be hidden at launch but then unhidden from the CCLayerClass in one of the methods.
If you want the button to be hidden at first then why are you setting it to hideButton= NO when you create it?

And what do you mean you "clicked the hidden property in viewDidLoad?" That doesn't make any sense.

The code I posted lets you control the state of the button when the view is displayed. If you set it to hideButton = YES, the button will be hidden. So set play.hideButton=YES, not NO.

I would have thought that would be obvious.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 08:50 PM   #17 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
If you want the button to be hidden at first then why are you setting it to hideButton= NO when you create it?

And what do you mean you "clicked the hidden property in viewDidLoad?" That doesn't make any sense.

The code I posted lets you control the state of the button when the view is displayed. If you set it to hideButton = YES, the button will be hidden. So set play.hideButton=YES, not NO.

I would have thought that would be obvious.
The view in which the pause button is in is technically holding the EAGLView but I think it is NULL at the point I am trying to change the .hidden property of the button from the CCLayerClass. I explain more below.

I am expecting that this will hide the button:
Code:
Play *play = [[[Play alloc] init] autorelease];
        play.hideButton = YES;
It did not. I will explain how my views are set up.
I switch views in View 1 my main view, that view isn't either of the two I am talking about now. My Play View has a IBOutlet of a EAGLView and Cocos2D is connected to it by a CCLayerClass. The UIButton is in the Play view above the EAGLView.
This is what I want/did:
I clicked the .hidden property in Interface Builder in my Play class (where the button is) so that when the init method of Cocos2D fires it will hide that button. When the user clicks a button in the CCLayerClass I want it to UN-Hide the button in the Play Class using this code but this doesn't work:
Code:
Play *play = [[[Play alloc] init] autorelease];
    play.hideButton = NO;
So yea that is my current situation.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8

Last edited by Objective Zero; 11-21-2011 at 08:53 PM.
Objective Zero is offline   Reply With Quote
Old 11-22-2011, 02:34 PM   #18 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Bump.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-22-2011, 03:40 PM   #19 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Objective Zero View Post
The view in which the pause button is in is technically holding the EAGLView but I think it is NULL at the point I am trying to change the .hidden property of the button from the CCLayerClass. I explain more below.

I am expecting that this will hide the button:
Code:
Play *play = [[[Play alloc] init] autorelease];
        play.hideButton = YES;
It did not. I will explain how my views are set up.
I switch views in View 1 my main view, that view isn't either of the two I am talking about now. My Play View has a IBOutlet of a EAGLView and Cocos2D is connected to it by a CCLayerClass. The UIButton is in the Play view above the EAGLView.
This is what I want/did:
I clicked the .hidden property in Interface Builder in my Play class (where the button is) so that when the init method of Cocos2D fires it will hide that button. When the user clicks a button in the CCLayerClass I want it to UN-Hide the button in the Play Class using this code but this doesn't work:
Code:
Play *play = [[[Play alloc] init] autorelease];
    play.hideButton = NO;
So yea that is my current situation.


This is a great example of why you shouldn't try to change another view controller's views directly.

After you create your play object, it's views don't exist yet. They don't get created until the view controller is about to be displayed.

That is why I told you to add code like the code below to your play view's viewWillAppear method:


Code:
- (void)viewWillAppear:(BOOL)animated
{
  //Update the button state in case it was changed 
  //before the view was loaded
  pauseButton.hidden = hideButton;
}
That code will be invoked right before the Play view controller appears on the screen. It will use the saved state of the hideButton property to set the button to the correct shown/hidden state.

A question:

When you run this code:

Code:
Play *play = [[[Play alloc] init] autorelease];
    play.hideButton = NO;

Is there already a Play object on the screen? If so, you probably shouldn't be creating another one.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-22-2011, 07:48 PM   #20 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
A question:

When you run this code:

Code:
Play *play = [[[Play alloc] init] autorelease];
    play.hideButton = NO;

Is there already a Play object on the screen? If so, you probably shouldn't be creating another one.
How would I tell? And you say if I already have one on the screen I shouldn't create another one so your saying that I should make it a ivar in my CCLayerClass?

And I did already have that one line of code in my viewWillAppear method
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-22-2011, 09:01 PM   #21 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Also should I setter and getter my UIButton because I think the issue is the UIButton being NULL even though it is appearing on screen?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 11-23-2011, 05:35 PM   #22 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Bump.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Reply

Bookmarks

Tags
button other class

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 389
15 members and 374 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja, Sami Gh
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:34 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0