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 09-21-2011, 07:13 PM   #1 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default Preserving UIButton state

So i have narrowed my problem down: I have 2 buttons, which are created in the viewDidLoad method, which also initializes what action methods they use, which appears after clicking on a table cell in the previous controller. After i click one i want them both to disable. I have achieved this. However, if someone navigates back to the table view, and then enters the same cell, the buttons are re-enabled. How can I preserve the state of these buttons as disabled if i have clicked one, and then after returning to the table view controller, if i click on the same cell, they remain disabled, but not if i click on a new cell?
gma is offline   Reply With Quote
Old 09-21-2011, 07:19 PM   #2 (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 gma View Post
So i have narrowed my problem down: I have 2 buttons, which are created in the viewDidLoad method, which also initializes what action methods they use, which appears after clicking on a table cell in the previous controller. After i click one i want them both to disable. I have achieved this. However, if someone navigates back to the table view, and then enters the same cell, the buttons are re-enabled. How can I preserve the state of these buttons as disabled if i have clicked one, and then after returning to the table view controller, if i click on the same cell, they remain disabled, but not if i click on a new cell?
Set up a boolean instance variable buttonsEnabled.

Create a method configureButtons that reads the buttonsEnabled iVar and uses it to either enable or disable your buttons.

Make your button click method set buttonsEnabled to FALSE and then call configureButtons.

Also make your viewWillAppear method call configureButtons.

That way, when the view is getting ready to display, the buttons get set up with the saved buttonsEnabled state.
__________________
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 offline   Reply With Quote
Old 09-21-2011, 07:31 PM   #3 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

thank you very much for the reply, however, i think i will have a similar problem. You see, i have one viewController class called "VoteViewController" which makes 2 buttons and so forth, and gets its display information from my tableViewController class based on which cell you click. So if i my solitary view controller has 1 flag, then all the cell's that i click on will suffer the state that one before it did right?
gma is offline   Reply With Quote
Old 09-21-2011, 07:40 PM   #4 (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 gma View Post
thank you very much for the reply, however, i think i will have a similar problem. You see, i have one viewController class called "VoteViewController" which makes 2 buttons and so forth, and gets its display information from my tableViewController class based on which cell you click. So if i my solitary view controller has 1 flag, then all the cell's that i click on will suffer the state that one before it did right?
So make your enableButtons flag a property, and set the flag in the table view controller just before pushing the vote view controller. That way the table view controller can control the button state in the vote view controller.

Are you saying that each entry in your table view should remember the enabled/disabled state of the buttons that get displayed in the vote view controller? In that case, you'd need to create an array of flags, one for each entry in your table, and use that to set the state of the enableButtons property before displaying your vote view controller. Make sense?
__________________
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 offline   Reply With Quote
Old 09-21-2011, 07:44 PM   #5 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

Thank you again, but that last thing you described is EXACTLY what i started coding before i read this! but thank you very much! i will see if it works!
gma is offline   Reply With Quote
Old 09-22-2011, 04:49 PM   #6 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

still problems.... so i have 2 ints decalared in my VoteViewController class that handle how big the mutable array of flags should be and the current row that it came from in the previous controller. however, now the buttons just start off disabled and stay that way, even tho i initialize the array as such:

for(int i=0;i<numberOfRows; i++){
[buttonStates addObject:[NSNumber numberWithBool: YES]];
NSLog(@"%d", [[buttonStates objectAtIndex:i]boolValue]);
}

all i see in out put is 0,0,0,0,0,0, when it is CLEARLY supposed to YES, or 1.

where should i initialize this array, because it seems in viewDidLoad i will either have the same array everytime, or it wont work it all like its doing....
gma is offline   Reply With Quote
Old 09-22-2011, 05:10 PM   #7 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

Good god, i have a check statement that asks what the value is before i insert it and it says 1... GOD I HATE OBJECTIVE C THIS IS SO STUPID
gma is offline   Reply With Quote
Old 09-22-2011, 05:20 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 gma View Post
still problems.... so i have 2 ints decalared in my VoteViewController class that handle how big the mutable array of flags should be and the current row that it came from in the previous controller. however, now the buttons just start off disabled and stay that way, even tho i initialize the array as such:

for(int i=0;i<numberOfRows; i++){
[buttonStates addObject:[NSNumber numberWithBool: YES]];
NSLog(@"%d", [[buttonStates objectAtIndex:i]boolValue]);
}

all i see in out put is 0,0,0,0,0,0, when it is CLEARLY supposed to YES, or 1.

where should i initialize this array, because it seems in viewDidLoad i will either have the same array everytime, or it wont work it all like its doing....
When do you alloc/init the buttonStates array? It sounds to me like buttonStates is nil. (you can send any message to a nil object and the result you get back is 0 {or nil}.)
__________________
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 offline   Reply With Quote
Old 09-22-2011, 05:24 PM   #9 (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 gma View Post
Good god, i have a check statement that asks what the value is before i insert it and it says 1... GOD I HATE OBJECTIVE C THIS IS SO STUPID
To quote Hal from "2001, a Space Odyssey":

"Sit down, take a stress pill, and think things through."

You can display an array directly, and it will show you each element in the array. So after your loop is finished, you could add a log statement like this:

Code:
NSLog(@"buttonStates array = %@", buttonStates);
My guess is that you forgot to initialize the array, and it's nil.
__________________
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 offline   Reply With Quote
Old 09-22-2011, 05:26 PM   #10 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

I tried [buttonStates alloc] right above it. im about to throw this computer out the window.
gma is offline   Reply With Quote
Old 09-22-2011, 05:36 PM   #11 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

alright, so buttonStates = [NSMutableArray array] worked, got some unrecognized selector problems now but ill report back.
gma is offline   Reply With Quote
Old 09-22-2011, 05:58 PM   #12 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

ok, so in my button click methods, when i add the code

[buttonStates replaceObjectAtIndex: currentRow withObject:[NSNumber numberWithBool:NO]];

crashes the application with no error.

I added a global breakpoint and ran again, and all i get is EXC_BAD_ACCESS
gma is offline   Reply With Quote
Old 09-22-2011, 06:00 PM   #13 (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 gma View Post
alright, so buttonStates = [NSMutableArray array] worked, got some unrecognized selector problems now but ill report back.
Post the header where you declare buttonStates, as well as the code that alloc/inits the variable.

Here's the deal.


When you declare an instance variable, it's just a pointer. The system sets that pointer to nil (or zero).

You have to create an object and set the pointer to point to the newly created object.

Your code

Code:
buttonStates = [NSMutableArray array];
creates a mutable array object, but it creates an autorleased object. That's a temporary object that will go away unless somebody takes ownership of it by sending it a retain message. So in the line above, buttonStates now contains a pointer to a mutable array that has no owner, and is slated for destruction.

You should probably make buttonStates a retained property, and then use code like this:

Code:
self.buttonStates = [NSMutableArray arrayWithCapacity: 50];
Where the value for capacity (50 in this case) is a guess as to how big the array is going to grow. If you get guess exactly, great. If not, the array will grow to accommodate more objects, but at the cost of taking extra time to do so.

By making buttonStates a property declared like this:

Code:
@property (nonatomic, retain) NSMutableArray* buttonStates;
You tell the system to generate glue code (a "setter") that retains any object you pass in to the property. (and release any old object stored in the property.)

Then, if you add this line to your dealloc method:

Code:
self.buttonSates = nil;
You release any object that was in buttonStates before your object is deallocated, and avoid a memory leak.

The reason that you are having a frustrating time is that you haven't learned the basics. You're slapping together code without understanding the principles behind it, and that doesn't work. Computers are really, really picky. You have to get things perfect, or the program fails - often spectacularly.
__________________
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.

Last edited by Duncan C; 09-22-2011 at 06:03 PM.
Duncan C is offline   Reply With Quote
Old 09-22-2011, 06:00 PM   #14 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

alright i did [buttonStates retain] in my viewDidLoad method and it worked, but now i am back to my god damned original problem, when it reloads the buttons are re-enabled....
gma is offline   Reply With Quote
Old 09-22-2011, 06:05 PM   #15 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

I do have it declared as such in my .h
{
NSMutableArray *buttonStates;
}

@property(nonatomic, retain) NSMutableArray *buttonStates;


and i will agree i do not have the basics of all the memory management. however, this is the way i learn with trial and error. So i am in the process of learning the basics, i just do not want to read a book, that's not how i learn. I have to actively read/do it. however i have everything declared as you said i should. now my problem is that the viewDidLoad is resetting my array.(i think).

by the way thank you so much for your continued response and patience.

Last edited by gma; 09-22-2011 at 06:16 PM.
gma is offline   Reply With Quote
Old 09-22-2011, 07:55 PM   #16 (permalink)
gma
Registered Member
 
Join Date: Sep 2011
Posts: 15
gma is on a distinguished road
Default

OKAY ! good news. What i did was in the previous table Controllers ViewDidLoad method I initialize the array with all YES. When i push the new view, i set a mutable array to be that of the array i loaded in the previous view.

Then, when i click a button, I set them disabled, replace the index in the array with the row that had clicked it with NO, then i go back into and it is still disabled and displaying my votes! Now, in my viewDidUnload method of the VoteViewController class, I set the array in the previous viewcontroller to be the array that i just changed. (i can forsee too much memory being used to do a simple task, but this working now is good enough for me). Since i have the 2 classes in a navigation controller, it doesnt reload the Array with YES's becuase the tableViewController viewDidLoad is not called again when navigating back. I had to do some fancy workarounds with another class i needed the same behaviour with, which involved giving it a whole array of stuff when all i need is an int value, but oh well.

If you see a huge glaring problem, please let me know, otherwise its working as i had wanted(despite possible memory issues).
gma is offline   Reply With Quote
Reply

Bookmarks

Tags
setenabled, uibutton, uitableviewcell

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: 395
14 members and 381 guests
7twenty7, eski, EvilElf, HemiMG, iOS.Lover, jarv, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

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