2

我有三个这样的按钮,每个按钮对应一个不同的按钮UIViewController

在此处输入图像描述

例如,我按下按钮 1。它将向开始按钮传递与它将显示的那个数字相对应的视图控制器。

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

在此处输入图像描述

可能吗?

4

2 回答 2

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 时在控制器中创建的代码

新视图控制器.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
}
于 2012-05-30T01:55:04.400 回答
1

如果我正确理解了这个问题:让我们谈谈按钮一(您可以将其推广到其他按钮和 VC)。假设关联的 viewController 称为 button1ViewController。

在您的 button1ViewController 中,声明一个名为 button 的属性(@property(strong,non atomic) UIButton *button - 也合成)..

在您的 IBAction 方法中。

-(IBAction) button1Tapped:(id)sender {

button1ViewController.button = sender;

[self presentViewController:button1ViewController;

}

编辑:我想我现在得到了这个问题。

在你的 mainView 控制器中声明一个实例变量:UIViewController *pushedController。同时声明实例 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];

}

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

于 2012-05-30T03:11:57.403 回答