【UIViewController界面传值】

AppDelegate.m

#import "AppDelegate.h"

#import "ViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    ViewController *v=[[ViewController alloc]init];

    self.window.rootViewController=v;

    [self.window makeKeyAndVisible];

    

    return YES;

}


ViewController.h

#import <UIKit/UIKit.h>

#import "otherViewController.h"


@interface ViewController : UIViewController<passVaule>

{

    UITextField *text;

}

@end


#import "ViewController.h"

#import "otherViewController.h"


@interface ViewController ()


@end


@implementation ViewController


ViewController.m

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor greenColor];

    

    text=[[UITextField alloc]initWithFrame:CGRectMake(1108011040)];

    text.backgroundColor=[UIColor yellowColor];

    [self.view addSubview:text];

    

    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(11013011040)];

    btn.backgroundColor=[UIColor yellowColor];

    [btn setTitleColor:[UIColor blackColorforState:UIControlStateNormal];

    [btn setTitle:@"切换界面" forState:UIControlStateNormal];

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

    [self.view addSubview:btn];

    

}

-(void)setString:(NSString *)_str

{

    text.text=_str;

}

-(void)shiftTap

{//前提:头文件引入otherviewcontroller.h  otherviewcontroller.h里声名了str属性

    otherViewController *pic=[[otherViewController alloc]init];

    pic.str=text.text;

    pic.delegate=self;

    [self presentViewController:pic animated:YES completion:nil];

   


}


otherViewController.h

#import <UIKit/UIKit.h>

@protocol passVaule

-(void)setString:(NSString *)_str;

@end

@interface otherViewController : UIViewController<passVaule>

{

    UITextField *text1;

}

@property(strong,nonatomic)NSString *str;

@property(strong,nonatomic)id<passVaule> delegate;

@end



otherViewController.m

#import "otherViewController.h"


@interface otherViewController ()


@end


@implementation otherViewController




- (void)viewDidLoad {

    [super viewDidLoad];

    

    UIImage *ima=[UIImage imageNamed:@"start1.jpg"];

    [self.view addSubview:[[UIImageView alloc]initWithImage:ima]];

    

    

    text1=[[UITextField alloc]initWithFrame:CGRectMake(1108011040)];

    text1.backgroundColor=[UIColor yellowColor];

    //text1.text=self.str;

    text1.text=_str;

    [self.view addSubview:text1];

    

    

    UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(11013011040)];

    btn1.backgroundColor=[UIColor yellowColor];

    [btn1 setTitleColor:[UIColor blackColorforState:UIControlStateNormal];

    [btn1 setTitle:@"切换界面" forState:UIControlStateNormal];

    [btn1 addTarget:self action:@selector(shiftTap) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn1];

  


}



-(void)shiftTap

{

    [self.delegate setString:text1.text];//相当于_strtext1.text

    [self dismissViewControllerAnimated:YES completion:nil];//销毁此视图

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值