原帖地址 :http://furnacedigital.blogspot.com/2011/12/storyboard-segue-uiviewcontroller_30.html
1
2
3
|
- (IBAction)returnToFirstPage:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{}];
}
|
1
2
3
|
@property (weak) NSString *string;
//別忘了在對應的實作檔中加入@synthesize string;
|
1
2
3
4
5
6
|
- (void)viewDidLoad
{
[super viewDidLoad];
page2TextField.text = string;
}
|
1
2
3
4
5
6
7
8
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//將page2設定成Storyboard Segue的目標UIViewController
id page2 = segue.destinationViewController;
//將值透過Storyboard Segue帶給頁面2的string變數
[page2 setValue:page1TextField.text forKey:@"string"];
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#import <UIKit/UIKit.h>
//建立一個協定
@protocol Page2Delegate
//協定中的方法
- (void)passValue:(NSString *)value;
@end
@interface Page2ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *page2TextField;
@property (weak) NSString *string;
//宣告一個採用Page2Delegate協定的物件
@property (weak) id<Page2Delegate> delegate;
@end
|
1
2
3
4
5
6
7
|
- (IBAction)returnToFirstPage:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{}];
//呼叫協定中的方法並帶入page2textField的數值
[delegate passValue:page2TextField.text];
}
|
1
2
3
4
5
6
7
8
9
|
#import <Uikit/Uikit.h>
//引用持有Page2Delegate協定的class
#import "Page2ViewController.h"
@interface MLViewController : UIViewController <page2delegate> //採用協定
@property (weak, nonatomic) IBOutlet UITextField *page1TextField;
@end
|
1
2
3
4
5
|
- (void)passValue:(NSString *)value {
//設定page1TextField為所取的的數值
page1TextField.text = value;
}
|
1
2
3
4
5
6
7
8
9
10
11
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//將page2設定成Storyboard Segue的目標UIViewController
id page2 = segue.destinationViewController;
//將值透過Storyboard Segue帶給頁面2的string變數
[page2 setValue:page1TextField.text forKey:@"string"];
//將delegate設成自己(指定自己為代理)
[page2 setValue:self forKey:@"delegate"];
}
|