IOS各種傳值方式

來源:互聯網
上載者:User

標籤:

屬性傳值 將A頁面所擁有的資訊通過屬性傳遞到B頁面使用

B頁面定義了一個naviTitle屬性,在A頁面中直接通過屬性賦值將A頁面中的值傳到B頁面。

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface RootViewController :UIViewController<ChangeDelegate>

{

    UITextField *tf;

}

@end

 

A RootViewController.m頁面實現檔案

#import "RootViewController.h"

#import "DetailViewController.h"

 

@interface RootViewController ()

@end

@implementation RootViewController

//核心代碼

-(void)loadView

{

 

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(0, 0, 100, 30);

    [btn setTitle:@"Push" forState:0];

    [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

 

 

-(void)pushAction:(id)sender

{

    tf = (UITextField *)[self.viewviewWithTag:1000];

    //導航push到下一個頁面

    //pushViewController 入棧引用計數+1,且控制權歸系統

    

    DetailViewController *detailViewController = [[DetailViewControlleralloc]init];

    //屬性傳值,直接屬性賦值

    detailViewController.naviTitle =tf.text;

    //導航push到下一個頁面

    [self.navigationControllerpushViewController:detailViewController animated:YES];

    [detailViewControllerrelease];   

}

 

B頁面DetailViewController.h檔案

 

#import <UIKit/UIKit.h>

@interface DetailViewController :UIViewController

{

   UITextField *textField;

   NSString *_naviTitle;

}

@property(nonatomic,retain)NSString *naviTitle;

@end

 

B頁面.m實現檔案

 

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

@synthesize naviTitle =_naviTitle;

-(void)loadView

{

    self.view = [[[UIViewalloc]initWithFrame:CGRectMake(0,0, 320,480)]autorelease];

   self.title = self.naviTitle ;    

}

 

代理傳值 

A頁面push到B頁面,如果B頁面的資訊想回傳(回調)到A頁面,用用代理傳值,其中B定義協議和聲明代理,A確認並實現代理,A作為B的代理

 

A頁面RootViewController.h檔案

 

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface RootViewController : UIViewController<ChangeDelegate>

{

    UITextField *tf;

}

@end

 

A頁面RootViewController.m實現檔案

#import "RootViewController.h"

#import "DetailViewController.h"

 

@interface RootViewController ()

@end

@implementation RootViewController

//核心代碼

-(void)loadView

{

 

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(0, 0, 100, 30);

    [btn setTitle:@"Push" forState:0];

    //A頁面push到B頁面

    [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

 

}

 

 

-(void)pushAction:(id)sender

{

    tf = (UITextField *)[self.view viewWithTag:1000];

    //導航push到下一個頁面

    //pushViewController 入棧引用計數+1,且控制權歸系統

    DetailViewController *detailViewController = [[DetailViewController alloc]init]; 

 

     //代理傳值

   detailViewController.delegate =self;//讓其自身作為代理人

    //導航push到下一個頁面

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

    [detailViewController release];   

}

 

//實現代理方法

-(void)changeTitle:(NSString *)aStr

{

    tf = (UITextField *)[self.view viewWithTag:1000];

    tf.text = aStr;//將從B頁面傳入的參數賦給A頁面中的TextField

   tf.text = aStr;

}

 

B頁面DetailViewController.m檔案

 

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

{

    UITextField *textField;

    //定義代理

 

  id<ChangeDelegate>_delegate;

}

 

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

@end

//定義協議

@protocol ChangeDelegate <NSObject>

-(void)changeTitle:(NSString *)aStr;//協議方法

@end

 

 

 

B頁面DetailViewController.h實現檔案

 

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

-(void)loadView

{

    self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];

 

    UIBarButtonItem *doneItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(doneAction:)];

    self.navigationItem.rightBarButtonItem = doneItem;

    [doneItemrelease];

}

 

//pop回前一個頁面

-(void)doneAction:(id)sender

{

   if (self.delegate && [self.delegaterespondsToSelector:@selector(changeTitle:)])//若代理存在且響應了changeTitle這個方法

    {

        //[self.delegate changeTitle:textField.text];

        [self.delegatechangeTitle:textField.text];//將textField.text參數傳給changeTitle方法  讓代理,也就是A頁面去實現這個方法

        NSLog(@"%@",self.navigationController.viewControllers);

        [self.navigationControllerpopViewControllerAnimated:YES];

    }

}

 

Block

幾種形式的Block

    //無傳回值
    void (^block1) (void);
    block1 = ^{
        NSLog(@"bock demo");
    };
    block1();
    
    //int傳回型別
    int (^block2) (void);
    block2  = ^(void)
    {
        int a  = 1 ,b =1;
        int c = a+b;
        return  c;
    };
    
    //有返回 有參數
    int (^block3)(int, int)= ^(int a, int b)
    {
        int c = a +b;
        return c;
        
    };
    NSLog(@"bock=%d",block3(1,2));
    
    //有傳回值,有參數並且可以修改block之外變數的block
    static int sum = 10;// __blcik and static關鍵字 或者 _block int sum = 10
    int (^block4) (int) =^(int a)
    {
        sum=11;
        int c = sum+a;   //此時sum就是可以修改的了,若沒加static或_block關鍵字則不能修改block之外變數
        return c;
    };
    NSLog(@"block4= %d",block4(4));

 

Block傳值

例如A(Ablock)頁面的值傳道B(Bblock)頁面  

 

在A頁面中ABlock.h

@interface Ablock : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableview;
    UILabel *labe;
    UIImageView *imagevies;
}
@end

 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableview deselectRowAtIndexPath:indexPath animated:YES];
    
    Bblcok *bblock = [[Bblcok alloc] initwithBlock:Block_copy(^(NSString *aBlock){    
        labe.text = aBlock;
        NSLog(@"%@",aBlock);

    })];
    

    bblock.imgeviews = imagevies.image;
    bblock.String = labe.text;
    [self.navigationController pushViewController:bblock animated:YES];
    [bblock release];
}

 

 

在A頁面中Bblock.h

#import <UIKit/UIKit.h>
typedef  void (^MyBlock) (NSString *);
@interface Bblcok : UIViewController
{
    UIImageView *image;
    UITextField *aField;
    UIButton *aButt;
    NSString *_String;
    id _imgeviews;
    MyBlock myBlock;
}
@property(nonatomic,copy)MyBlock myBlock;   
@property(nonatomic,retain) id imgeviews;
@property(nonatomic,retain) NSString *String;
-(id)initwithBlock:(MyBlock)aBlcok;
@end

 

//
//  Bblcok.m
//  Blcok
//
//  Created by zhu  on 13-8-12.
//  Copyright (c) 2013年 Zhu Ji Fan. All rights reserved.
//

#import "Bblcok.h"

@interface Bblcok ()

@end

@implementation Bblcok
@synthesize imgeviews = _imgeviews , String = _String;
@synthesize myBlock = _myBlock;
-(id)initwithBlock:(MyBlock)aBlcok
{
    if (self = [super init])
    {
        self.myBlock = aBlcok; 
    }
    return self;
}

-(void) dealloc
{
    [super dealloc];
}

-(void) loadView
{
    UIControl *cont = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 568-44)];
    [cont addTarget:self action:@selector(Clcik) forControlEvents:UIControlEventTouchUpInside];
    self.view = cont;
    
    aField = [[UITextField alloc] initWithFrame:CGRectMake(60, 10, 160, 30)];
    aField.borderStyle = UITextBorderStyleLine;
    aField.placeholder = self.String;
    [self.view addSubview:aField];
    
    aButt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    aButt.frame = CGRectMake(60, 50, 70, 30);
    [aButt setTitle:@"修改" forState:0];
    [aButt addTarget:self action:@selector(aButtClcik:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:aButt];
    
    image = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 210, 260)];
    image.backgroundColor = [UIColor blueColor];
    image.image = self.imgeviews;
    [self.view addSubview:image];
    [image release];

}

-(IBAction)aButtClcik:(id)sender
{
    NSString *sting = aField.text;
    myBlock(sting);
    [self.navigationController popToRootViewControllerAnimated:YES];
}


-(void)Clcik
{
    [aField resignFirstResponder];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

IOS各種傳值方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.