2012-07-11 62 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