2012-07-11 71 views

回答

0

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

在你UIView h文件,创建定义代表该视图的协议:

SomethingView.h:

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

... 

@interface SomethingView { 
    id<SomethingViewDelegate> delegate; 
} 

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

SomethingView.m:

@implementation 
@synthesize delegate; 

... 

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

MyViewController。米,您创建您的视图:

// 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]; 
} 
+0

有没有关于创建segue的示例?这是一个自定义的赛格吗? – 2012-07-11 05:50:08

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. 
} 
+0

我期待从一个视图控制器转到另一个按下UIView(子视图)上的按钮。那可能吗? – 2012-07-11 06:10:54

+0

这个子视图是一个外部.xib文件 – 2012-07-11 06:13:36

+0

是的,为什么不。你试一试 – 2012-07-11 06:16:55