【UIViewController界面传值】

本文介绍了一个简单的iOS应用程序启动过程及两个视图控制器之间的界面切换实现方式。通过AppDelegate设置初始视图,并在ViewController中创建按钮以触发到otherViewController的界面跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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];//销毁此视图

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值