界面的跳轉和傳值


//在APPDelegate.h中,即項目的入口部分

//didFinishLaunchingWithOptions該方法表示APP啓動完畢,接下來進入自定義界面

//如下MyViewController是自定義的類,即一個界面實現UIViewController接口

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    MyViewController *myController = [[MyViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myController];
    
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    
    return YES;
}
//MyViewController.h
//定義四個控件
@interface MyViewController : UIViewController

@property (strong, nonatomic) UILabel *labelNum;
@property (strong, nonatomic) UILabel *labelPwd;

@property (strong, nonatomic) UITextField *fieldNum;
@property (strong, nonatomic) UITextField *fieldPwd;

@end

 
//MyViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.labelNum = [[UILabel alloc] init];
    self.labelNum.frame = CGRectMake(100, 100, 50, 20);
    self.labelNum.text = @"Num: ";
    
    self.fieldNum = [[UITextField alloc] init];
    self.fieldNum.frame = CGRectMake(150, 100, 100, 20);
    self.fieldNum.borderStyle = UITextBorderStyleRoundedRect;
    
    self.labelPwd = [[UILabel alloc] init];
    self.labelPwd.frame = CGRectMake(100, 150, 50, 20);
    self.labelPwd.text = @"Pwd: ";
    
    self.fieldPwd = [[UITextField alloc] init];
    self.fieldPwd.frame = CGRectMake(150, 150, 100, 20);
    self.fieldPwd.borderStyle = UITextBorderStyleRoundedRect;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 200, 80, 20);
    btn.backgroundColor = [UIColor grayColor];
    [btn setTitle:@"login" forState:UIControlStateNormal];
    
    //設置按鈕監聽事件,監聽方法爲login
    [btn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchDown];
    
    
    [self.view addSubview:self.labelNum];
    [self.view addSubview:self.fieldNum];
    
    [self.view addSubview:self.labelPwd];
    [self.view addSubview:self.fieldPwd];
    
    [self.view addSubview:btn];
}


//頁面跳轉,並傳遞參數

-(void) login{
    NSLog(@"login");
    
    NSString *num = self.fieldNum.text;
    NSString *pwd = self.fieldPwd.text;
    
    if([num isEqualToString:@"123"] == TRUE){
        
        NSLog(@"yes");

        //初始化要跳轉的界面
        MainViewController *mm = [[MainViewController alloc] init];
        
        //把賬號密碼傳遞到下個界面
        [mm initData:num two:pwd];
        
        //開始跳轉
        [self.navigationController pushViewController:mm animated:FALSE];
    }else{
        NSLog(@"error");
    }
   
}


//MainViewController.h

@interface MainViewController : UIViewController
//@property( nonatomic) NSString

//傳遞兩個參數a、b NSString 類型
-(void) initData:(NSString *) a two:(NSString *) b;

@end
//接收參數,並保存
-(void) initData:(NSString *) a two:(NSString *) b{
    self.num = a;
    self.pwd = b;
    
    NSLog(@"%@,%@", self.num, self.pwd);
}


//效果圖

wKioL1eG9DuBJBUUAAG9p59ZXWs980.png-wh_50



wKiom1eG9CuzaD8tAAM13Mxk85k431.png-wh_50






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章