1

This is my action UIButton:

-(IBAction)favoriteButtonPressed:(id)sender { if (favoriteButtonSelected == 0) { [sender setSelected:YES]; favoriteButtonSelected = 1; [sender setImage:[UIImage imageNamed:@"favoritedItem.png"]]; [selectedObject setValue:@"Yes" forKey:@"Favorite"]; } else { [sender setSelected:NO]; favoriteButtonSelected = 0; [sender setImage:[UIImage imageNamed:@"notFavorite.png"]]; [selectedObject setValue:@"No" forKey:@"Favorite"]; } } 

How to make a reference to the button in viewDidLoad? To make the following code work:

- (void)viewDidLoad { [super viewDidLoad]; if ([[selectedObject valueForKey:@"Favorite"] isEqual:@"Yes"]) { [favoriteButton setImage:[UIImage imageNamed:@"favoritedItem.png"]]; [favoriteButton setSelected:YES]; favoriteButtonSelected = 1; } else { [favoriteButton setImage:[UIImage imageNamed:@"notFavorite.png"]]; [favoriteButton setSelected:NO]; favoriteButtonSelected = 0; } } 

EDIT FOR PROGRESS:

Now I did like this: Ctrl-drag from UIButton to ViewController in Assistant Editor. Connection: Outlet, name: favoriteButton, type: UIButton, storage: weak. But errors still there. + error for synthesize & error in viewDidUnload.. suggestion?

The Assistant Editor header for View Controller with the added property from Ctrl-drag:

@interface DetailViewController : UIViewController { IBOutlet UIScrollView *viewScroller; } @property (nonatomic, strong) IBOutlet UILabel *mylLabel; @property (nonatomic, strong) NSString *selectedObj; @property (strong, nonatomic) NSArray *detailsDataSource; @property int detailIndex; @property (weak, nonatomic) IBOutlet UIButton *favoriteButton; //The added property @end 

3 Answers 3

2

Control drag from your button to the header of the viewcontroller to create a property. (You can do this while in assistant mode, second button on the top right)..

Then you can reference your button from wherever using that property

Sign up to request clarification or add additional context in comments.

5 Comments

Ok thanks, I tried that but it didn't work.. Did like this: Ctrl-drag from UIButton to ViewController in Assistant Editor. Connection: Outlet, name: favoriteButton, type: UIButton, storage: strong. But errors still there. + error for synthesize & error in viewDidUnload.. suggestion?
there shouldnt be an error, if you did that on the header xcode automatically creates the synthesize and nils it in the viewdidunload. are you sure the name is the same?
the other option would be for you to create an IBOutlet by yourself on the header of that viewcontroller. make it like this @property (weak, nonatomic) IBOutlet UIButton *favoriteButton; Then control drag the button to the little box bellow the viewcontroller view, a list should appear and you will see favoriteButton there.
Yes, the name is the same. It adds the synthesize and nil, yes. But it adds no property in the .h. This property is only visible in assistant editor. Is this right behaviour? (I control drag from the button and creates the property in the window that pops up when I press the assistant editor button) see my edit.
i just noticed and your code itself is wrong, a button cannot be set like that. if u want to change the image it must be [[favoriteButton setImage:<#(UIImage *)#> forState:<#(UIControlState)#>] ]; also change it for the Action method.
2

What you need is an IBOutlet for the button.

You can ctrl+drag the button from XIB file to the header of this class, and create a IBOutlet property named favoriteButton.

2 Comments

Ok thanks, I tried that but it didn't work.. Did like this (using storyboard): Ctrl-drag from UIButton to ViewController in Assistant Editor. Connection: Outlet, name: favoriteButton, type: UIButton, storage: strong. But errors still there. + error for synthesize & error in viewDidUnload.. suggestion?
As Luis Oscar said, Xcode should add those. If not, try synthesize button property by yourself and set it to nil in viewDidUnload.
0

Well unless you are using the latest version of Xcode you do need to @synthesize favorite button.

You really should post your error messages you are just making people guess.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.