新手介紹簡單一下iOS開發中幾種界面傳值

首先在處理iOS-UI中,也許在不少地方須要用到兩種甚至多種不一樣界面之間的傳值,相比這也是不少iOS入門成員頭疼問題,一樣做爲新手的我在接觸這類傳值時候也一臉懵然,通過一段時間的研究,對於簡單的傳值有必定的瞭解,下面依次對屬性傳值,代理傳值作必定的說明,但願對於部分有須要的朋友有幫助。ide

1、屬性傳值:ui

經過圖片能夠看出,屬性傳值的是當前界面日後面的界面傳值。首先根據我的的須要建立兩個不一樣的界面,在前面的atom

UITextField中輸入須要的值而後點擊跳轉就能夠進入下一屆面,而後對應的值會顯示在下界面的提示框中:這裏用兩個例子(firstViewController,secondViewController),在secondViewController的.h文件中聲明屬性:設計

@property(nonatomic,strong)NSString *textString;(屬性傳值第一步)3d

而後在.m文件中聲明屬性代理

@property(nonatomic,strong)UILabel *label;orm

接着:對象

- (void)viewDidLoad {blog

    [super viewDidLoad];圖片

    self.view.backgroundColor = [UIColor whiteColor];

    //配置導航欄標題

    self.navigationItem.title = @"屬性";    

    [self uibuttongs];

    //建立lable

    [self customLable];

    //屬性傳值第三步

    //將屬性中存儲的值取出來賦值給_lable.text

    _label.text= _textString;

}

- (void)dealloc{

    self.textString = nil;

}

- (void)customLable{

    //建立lable對象

    _label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];

    _label.layer.cornerRadius = 7;

    _label.backgroundColor = [UIColor cyanColor];

    _label.textColor = [UIColor blackColor];

    [self.view addSubview:_label];

}

- (void)uibuttongs{

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 160, 60, 30)];

    button.backgroundColor =[UIColor lightGrayColor];

    [button setTitle:@"上一頁" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(acction_butong:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}

 - (void)acction_butong:(UIButton *)sender//這裏是返回上界面

{

    [self.navigationController popViewControllerAnimated:YES];

}

而後只須要在 firstViewController中的.m文件完成以下聲明屬性:

@property(nonatomic,strong)UITextField *textField;

而後:

- (void)viewDidLoad {

    [super viewDidLoad];

   

    self.view.backgroundColor = [UIColor whiteColor];

    //配置導航欄標題

    self.tabBarItem.title = @"屬性傳值";

    //建立導航欄右功能鍵

    [self customRightItem];

    //建立textField

    [self customTextFied];

}

#pragma mark - 建立導航欄右側按鈕

- (void)customTextFied{

    //建立控件

    _textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];

    _textField.borderStyle = UITextBorderStyleRoundedRect;

    _textField.layer.borderWidth = 1;

    [self.view addSubview:_textField];

}

#pragma mark - 建立導航欄右側按鈕

- (void)customRightItem{

    UIBarButtonItem *rightItem =[[UIBarButtonItem alloc]initWithTitle:@"跳轉" style:UIBarButtonItemStyleDone target:self action:@selector(handleRight:)];

    self.navigationItem.rightBarButtonItem = rightItem;

}

#pragma mark - 導航欄右側按鈕關聯方法

- (void)handleRight:(UIBarButtonItem *)sender{

    //建立跳轉頁面視圖

    scendViewController *scender = [[scendViewController alloc]init];

    //屬性傳值第二步

    //在push以前將——textField的值取出來賦值給second.textString

    scender.textString = _textField.text;

    [self.navigationController pushViewController:scender animated:YES];

}

 

2、代理傳值:

 a.後一個頁面(找代理) 制定協議

 b.寫delegate屬性

 c.在合適的時候, 讓代理執行協議方法

 d.前一個頁面(成爲代理) 創建關係

 e.遵照協議

 f.實現協議方法

 g.通知代理執行協議方法

 */

一、二、三、

這裏依舊用(firstViewController,secondViewController)兩個界面來作例子,具體的界面根據我的的需求來定義聲明。

從圖片不難看出代理傳值時一種傳值方式從後界面往前界面往前傳值的過程,首先在secondViewController的.h文件中制定協議;

@protocol senderViewControllerDelegate <NSObject>

//代理傳值第一步:制定協議

//根據要傳的數據類型設計方法的參數

- (void)passValue:(NSString *)string;

@end

而後定義屬性:

@property(nonatomic,assign)id<senderViewControllerDelegate>delegate;

//代理傳值第二步,定義代理協議屬性

而後在其.m文件中封裝的屬性:

@property(nonatomic,strong)UITextField *textField;

接着:

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(hengde:)];

    _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 220, 40)];

    _textField.layer.borderWidth = 1;

    [self.view addSubview:_textField];

}

- (void)hengde:(UIBarButtonItem *)sender{

    //代理傳值第六步:在pop以前代理對象執行協議中的方法

    if ([_delegate respondsToSelector:@selector(passValue:)]) {

        [_delegate passValue:_textField.text];

    }

    [self.navigationController popViewControllerAnimated:YES];

}

完成在 secondViewController的操做後,在firstViewController中.m文件的操做:

//代理傳值第四步:代理對象所在的類遵循協議

@interface firstViewController ()<senderViewControllerDelegate>

@property(nonatomic,strong)UILabel *lable;//封裝屬性

@end

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.title = @"代理";

    _lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];

    _lable.layer.borderWidth = 1;

    [self.view addSubview:_lable];

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(130, 180, 100, 50)];

    [button setTitle:@"下一頁" forState:UIControlStateNormal];

     [button addTarget:self action:@selector(hended:) forControlEvents:UIControlEventTouchUpInside];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [self.view addSubview:button];

}

- (void)hended:(UIBarButtonItem *)sender{

    senderViewController *sendedfr = [[senderViewController alloc]init];

        //代理傳值第三步:指定代理對象

    sendedfr.delegate = self;

    [self.navigationController pushViewController:sendedfr animated:YES];

}

//代理傳值第五步:實現協議方法

- (void)passValue:(NSString *)string{

    _lable.text = string;

}

 而後代理的簡單傳值方式大概即便這樣!

 

附:別忘了在firstViewController或者secondVIewController中導入對應的文件.h文件!

相關文章
相關標籤/搜索