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(110, 80, 110, 40)];
text.backgroundColor=[UIColor yellowColor];
[self.view addSubview:text];
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(110, 130, 110, 40)];
btn.backgroundColor=[UIColor yellowColor];
[btn setTitleColor:[UIColor blackColor] forState: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];
}
#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(110, 80, 110, 40)];
text1.backgroundColor=[UIColor yellowColor];
//text1.text=self.str;
text1.text=_str;
[self.view addSubview:text1];
UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(110, 130, 110, 40)];
btn1.backgroundColor=[UIColor yellowColor];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn1 setTitle:@"切换界面" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(shiftTap) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
-(void)shiftTap
{
[self.delegate setString:text1.text];//相当于_str=text1.text
[self dismissViewControllerAnimated:YES completion:nil];//销毁此视图
}