0

我有一个 UIView .xib 文件。这是从故事板入口点 UIViewController1 作为子视图打开的。子视图有一个 IBButton,按下它会打开一个 UIViewController2。这有可能吗?

4

2 回答 2

0

首先,创建一个从第一个视图控制器到第二个视图控制器的 segue。我要给它命名OpenViewController2。我们将能够以编程方式调用该 segue。

在您的UIView.h 文件中,创建一个为该视图定义委托的协议:

东西视图.h:

@protocol SomethingViewDelegate <NSObject> {
    - (void)importantThingHappened;
}

...

@interface SomethingView {
    id<SomethingViewDelegate> delegate;
}

@property (nonatomic, strong) id<SomethingViewDelegate> delegate;

东西视图.m:

@implementation
@synthesize delegate;

...

// The IBAction for the button in your view
- (IBAction)buttonClicked:(id)sender {
    [delegate importantThingHappened];
}

MyViewController.m,在其中创建视图:

// Create view
UIView *myView = ...

myView.delegate = self;

稍后在 MyViewController 中:

// Implement the protocol. This function will be called when the button action
// inside of the UIView you created is pressed
- (void)importantThingHappened {
    [self performSegueWithIdentifier:@"OpenViewController2" sender:self];
}
于 2012-07-11T04:38:54.397 回答
0

首先给你的 IBButton 一个唯一的标签,然后在你的 UIViewController 的 viewDidLoad 中,

// add following line into viewDidLoad

[(UIButton*)[self.view viewWithTag:MY_UNIQUE_TAG_FOR_BUTTON] addTarget:self action:@selector(buttonPressed:) forControlEvent:UIControlEventTouchUpInside];

最后实现 buttonPressed: 任何你想要的

-(void) buttonPressed:(UIButton*)aButton {
    // do what you want to do.
}
于 2012-07-11T05:55:42.733 回答