2012-05-30 60 views
2

传递的UIViewController到另一个按钮我有三个按钮这样每个对应于不同的UIViewController在另一个的ViewController

enter image description here

例如,我按下按钮1.将传递到启动按钮的视图控制器对应于它将显示的数字。

我需要能够将它传递在此按钮:

enter image description here

这可能吗?

回答

1

是的,只需在按钮水龙头上显示的视图控制器上创建UIButton属性即可。然后,在按钮点击的处理程序中,使用新控制器上的按钮属性在显示新控制器之前分配按钮。

示例代码:

//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; 
} 

这里是按下按钮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 
} 
+0

你能展示一些样品吗? – Bazinga

+0

newVc.startButton - 找不到我的其他VC – Bazinga

1

如果我理解正确的问题是:让我们来谈谈对按钮一个(你可以推广到其他按钮和VC)。假设相关的viewController被称为button1ViewController。

在你button1ViewController,声明属性称为按钮(@property(强,非原子)的UIButton *按钮 - 也能合成)..

在你IBAction为方法。

-(IBAction) button1Tapped:(id)sender { 

button1ViewController.button = sender; 

[self presentViewController:button1ViewController; 

} 

编辑:我想我现在得到的问题。

在你的mainView控制器中声明一个实例变量:UIViewController * pushingController。 还声明实例Button1ViewController * button1ViewController; ,Button2ViewController * button2ViewController,Button3ViewController * button3ViewController。

现在:

-(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]; 

} 

我认为这应该工作,但是我还没有尝试过的代码我自己。

+0

我将在startButton中实现什么? – Bazinga

+0

我现在对这个问题感到困惑,你想将视图控制器传递给按钮吗? –

+0

是的。开始按钮位于不同的视图控制器中。当按下按钮1时,当按下StartButton时VIEW1将出现。但是当按下按钮2时,当按下StartButton时VIEW2将出现。现在得到它? – Bazinga

相关问题