溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

屬性傳值和協(xié)議傳值  

發(fā)布時間:2020-07-13 12:41:26 來源:網(wǎng)絡(luò) 閱讀:533 作者:Im劉亞芳 欄目:開發(fā)技術(shù)

屬性傳值三部.....

1.在第二個頁面.h中,定義name

//屬性傳值............1

@property (nonatomic, copy)NSString *name;

2.在第一頁.m中然后在推出第二個頁面前,把第一個按鈕的title值傳給第二個頁面定義的name

SecondViewController *secondVC = [[SecondViewController alloc] init];

    //屬性傳值..............2

    secondVC.name = button.currentTitle//把按鈕名給第二個頁面的name

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

    [secondVC release];

3.在第二頁.m中讓TextField來接收第一頁傳過來的值

//屬性傳值........3

    self.TextField.text = self.name;   //接收從第一頁傳過來的name


協(xié)議傳值六部走........

協(xié)議傳值六個步驟分別是在第二個頁面三部,在第一個頁面三個;

1.首先在第二頁的.h中自定義一個協(xié)議SecondPassValueDelegate,然后在里面寫一個方法,用來改變按鈕的title

//協(xié)議傳值----1;

//指定一個協(xié)議

@protocol SecondPassValueDelegate <NSObject>


- (void)changeButtonTitle:(NSString *)title;


@end


2.在第二頁.h中,定義一個代理人對象屬性(記得為id類型,并且是assign)

//協(xié)議傳值----2

//定一個代理人對象屬性

//只有實(shí)現(xiàn)了上面定義的協(xié)議方法的對象,才能為第二個頁面的代理人

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


3.在第二頁.m方法中 ,讓自己的代理人去執(zhí)行約定好的方法,獲得里面的值

/協(xié)議傳值----3

    //讓自己的代理人(delegate)去執(zhí)行約定好的方法  獲取里面的值

    [self.delegate changeButtonTitle:_TextField.text];

4.在第一個頁面簽訂第二個頁面的協(xié)議 ,對應(yīng)第一步的創(chuàng)建協(xié)議    (這里的協(xié)議可以在.h中寫,也可以在.m中私有方法實(shí)現(xiàn).---這里是后者 .)

//協(xié)議傳值-----4

//第一個頁面簽訂第二個頁面的協(xié)議   對應(yīng)第一步的創(chuàng)建協(xié)議


@interface FirstViewController () <SecondPassValueDelegate>


@end

5.在第一個頁面的.m方法中 ,把自己指定為第二個頁面的代理人

//把自己指定為第二個頁面的代理人

    secondVC.delegate = self;

6.在第一個頁面中實(shí)現(xiàn)協(xié)議的方法

- (void)changeButtonTitle:(NSString *)title

{

    //獲得按鈕

    UIButton *button = (UIButton *)[self.view viewWithTag:1000];

    //給按鈕 重新設(shè)置標(biāo)題

    [button setTitle:title forState:UIControlStateNormal];

    

    UIButton *button1 = (UIButton *)[self.view viewWithTag:2000];

    [button1 setTitle:title forState:UIControlStateNormal];

}


 

類和文件

AppDelegate.m

#import "AppDelegate.h"
#import "FirstViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
    self.window.rootViewController = naviVC;
    [firstVC release];
    
    [_window release];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end


FirstViewController.h

#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"
//協(xié)議傳值-----4
//第一個頁面簽訂第二個頁面的協(xié)議   對應(yīng)第一步的創(chuàng)建協(xié)議
@interface FirstViewController () <SecondPassValueDelegate>
@end
@implementation FirstViewController
- (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.
    
    self.title = @"第一頁";
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(120, 100, 80, 25)];
    [button setTitle:@"按鈕" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    button.tag = 1000;
    button.backgroundColor = [UIColor cyanColor];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button release];
    
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(120, 160, 80, 25)];
    [button1 setTitle:@"asdsadf" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    button1.layer.cornerRadius = 5;
    button1.tag = 2000;
    button1.backgroundColor = [UIColor blueColor];
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    [button1 release];
    
}
- (void)buttonClicked:(UIButton *)button
{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    //屬性傳值..............2
    secondVC.name = button.currentTitle;  //把按鈕名給第二個頁面的name
    //協(xié)議傳值------5
    //把自己指定為第二個頁面的代理人
    secondVC.delegate = self;
    
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}
//協(xié)議傳值-------6
//實(shí)現(xiàn)協(xié)議方法
- (void)changeButtonTitle:(NSString *)title
{
    //獲得按鈕
    UIButton *button = (UIButton *)[self.view viewWithTag:1000];
    //給按鈕 重新設(shè)置標(biāo)題
    [button setTitle:title forState:UIControlStateNormal];
    
    UIButton *button1 = (UIButton *)[self.view viewWithTag:2000];
    [button1 setTitle:title forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end



SecondViewController.h

#import <UIKit/UIKit.h>
//協(xié)議傳值----1;
//指定一個協(xié)議
@protocol SecondPassValueDelegate <NSObject>
- (void)changeButtonTitle:(NSString *)title;
@end
@interface SecondViewController : UIViewController
@property (nonatomic , retain)UITextField *TextField;
//屬性傳值............1
@property (nonatomic, copy)NSString *name;
//協(xié)議傳值----2
//定一個代理人對象屬性
//只有實(shí)現(xiàn)了上面定義的協(xié)議方法的對象,才能為第二個頁面的代理人
@property (nonatomic , assign)id <SecondPassValueDelegate>delegate;
@end

SecondViewController.m

#import "SecondViewController.h"
@interface SecondViewController () <UITextFieldDelegate>
@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.
    
    
    self.title = @"第二頁";
    self.view.backgroundColor = [UIColor brownColor];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(120, 150, 100, 35)];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    button.backgroundColor = [UIColor magentaColor];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    self.TextField = [[UITextField alloc] initWithFrame:CGRectMake(80, 80, 160, 50)];
    self.TextField.backgroundColor = [UIColor whiteColor];
    self.TextField.delegate = self;
    self.TextField.clearButtonMode = UITextFieldViewModeAlways;
    //屬性傳值........3
    self.TextField.text = self.name;   //接收從第一頁傳過來的name
    self.TextField.borderStyle =UITextBorderStyleRoundedRect;   //TextField的邊框圓形的邊框
    [self.view addSubview:self.TextField];
    [_TextField release];
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
    
}
- (void)buttonClicked:(UIButton *)button
{
    //協(xié)議傳值----3
    //讓自己的代理人(delegate)去執(zhí)行約定好的方法  獲取里面的值
    [self.delegate changeButtonTitle:_TextField.text];
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI