Reputation: 2466
I have three buttons like this each corresponds to a different UIViewController
:
For example, I press button 1. It will pass to the start button the view controller corresponding to that number that it will show.
I need to be able to pass it in this button:
Is it possible?
Upvotes: 2
Views: 291
Reputation: 12832
Yes, just create the UIButton as property on the view controller shown on button tap. Then, in the handler for the button tap, use the button property on the new controller to assign the button before showing the new controller.
Sample code:
//here's your method invoked when pressing the button 1
- (IBAction) pressedButtonOne:(id) sender {
NewViewController *newVc = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
newVc.startButton = self.startButton;
}
Here's the code in the controller created when pressing button 1
NewViewController.h
#import <UIKit/UIKit.h>
@interface NewViewController : UIViewController
@property (nonatomic, retain) UIButton *startButton;
//NewViewController.m
- (void) viewDidLoad {
//use self.startButton here, for whatever you need
}
Upvotes: 1
Reputation: 14841
If I understand the question correctly: Let's talk for button one (you can generalize it to the other buttons and VC's). Say the associated viewController is called button1ViewController.
In your button1ViewController, declare a property called button (@property(strong,non atomic) UIButton *button - also synthesize)..
In your IBAction method.
-(IBAction) button1Tapped:(id)sender {
button1ViewController.button = sender;
[self presentViewController:button1ViewController;
}
Edit: I think I get the question now.
In your mainView controller declare an instance variable: UIViewController *pushedController. Also declare instances Button1ViewController *button1ViewController; , Button2ViewController *button2ViewController, Button3ViewController *button3ViewController.
Now:
-(IBAction) button1Pressed:(id)sender {
if (button1ViewController==nil) {
button1ViewController = [Button1ViewController alloc] init];
}
pushedController = button1ViewController;
}
-(IBAction) button2Pressed:(id)sender {
if (button2ViewController==nil) {
button2ViewController = [Button2ViewController alloc] init];
}
pushedController = button2ViewController;
}
//same thing for button 3 pressed with button 3 controller
-(IBAction) startButtonPressed:(id) sender {
if (pushedViewController!=nil)
[self presentViewController:pushedViewController animated:YES completion:NULL];
}
I think this should work, however I haven't tried the code myself.
Upvotes: 1