【UIViewController界面传值】

来源:互联网 发布:追光者 知乎 编辑:程序博客网 时间:2024/06/04 09:01

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
原创粉丝点击