前一篇的實作使用了Navigation Bar來做聯結,現在要嘗試不要使用Navigation Bar,亦不用Segue。
實作如下
1. 開啓一個新的專案
2. 一樣加入一個新的UIViewController 的Class檔
3. 在storyboard上加入一個Button,來作啓動
4. 現在先從mainViewController.h開始
#import <UIKit/UIKit.h>
#import "secondViewController.h"
@interface mainViewController : UIViewController
@property (nonatomic) secondViewController *secondView;
@end
5.在mainViewController.m加入啓動second ViewController程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize secondView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)toSecondViewController:(id)sender {
secondView = [[secondViewController alloc] init];
// secondView.view.backgroundColor = [UIColor yellowColor];
NSString *Stringvalue = [NSString stringWithFormat:@"%d",345];
[secondView setValue:Stringvalue forKey:@"transferString"];
[self presentViewController:secondView animated:YES completion:^{}];
}
@end
6. 在secondViewController.h加入所需傳送變數及呼叫的Function定義。
#import <UIKit/UIKit.h>
@interface secondViewController : UIViewController
{
NSString *transferString;
}
- (void) goBackMainViewController;
@end
7. 在secondViewController.m加入顯示及回歸的程式碼
#import "secondViewController.h"
@interface secondViewController ()
@end
@implementation secondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"%@", transferString);
self.view.backgroundColor = [UIColor yellowColor];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setFrame:CGRectMake(300, 300, 75, 37)];
aButton.backgroundColor = [UIColor redColor];
[aButton setTitle:transferString forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(goBackMainViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) goBackMainViewController
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
@end
8. 顯示結果





沒有留言:
張貼留言