xcode4.3.1 怎样实现两个页面之间传值

2026年09月27日 07:55
有2个网友回答
网友(1):

传值一般分为两大类:

1、从前往后传值

 该种情况,一般是初始化ViewController的时候传递参数。

#import "EVNThirdViewController.h"
@interface EVNThirdViewController ()
{
   NSString *Trandata;//接收上个View传递来的参数
}
@end
@implementation EVNThirdViewController
-(instancetype)initWithTranValue:(NSString *)StrData
{
    self = [super init];
    if (self)
    {
       Trandata = StrData;//直接传值就可以
    }
    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

2、从后往前传值

这种情况就相对麻烦些,需要使用Delegate、或者Block进行回调传值。

使用Block进行页面的传值,只需要在需要的时候,回调Block就可以。这就是Block的神奇之处。
要实现的功能:第一个页面接收第二个页面传来的参数。

页面一:
#import "EVNFirViewController.h"
@interface EVNFirViewController ()
@end
@implementation EVNFirViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"EVNFirView";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(goNext:)];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回Fir" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 70)];
    self.label.textColor = [UIColor redColor];
    self.label.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.label];
}
- (void) goNext:(UIBarButtonItem *)sender
{
    EVNSecViewController *sec = [[EVNSecViewController alloc]init];
    
   
    sec.myblock = ^(NSString *str){
        self.label.text = str;
    };
    /*[sec tranData:^(NSString *str) {
        self.label.text = str;
        NSLog(@"str is value:%@",str);
    }];*/
    [self.navigationController pushViewController: sec animated:YES];
    
}
@end
页面二:
#import 
#import "EVNSecViewController.h"
typedef void (^MyBlock)(NSString *str);
@interface EVNSecViewController : UIViewController
@property (copy, nonatomic) MyBlock myblock;
/*-(void)tranData:(MyBlock)block;//这个地方不是必须这样,只是为了容易理解。*/
@end
#import "EVNSecViewController.h"
@interface EVNSecViewController ()
@end
@implementation EVNSecViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"SecView";
    [self.view setBackgroundColor:[UIColor brownColor]];
    UIBarButtonItem *rightBarbtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(BackFirView:)];
    self.navigationItem.rightBarButtonItem = rightBarbtn;
}
- (void) BackFirView:(UIBarButtonItem *)sender
{  
    if (self.myblock !=nil)
    {
        self.myblock(@"asdfasdf");//传回前一个ViewController的值。
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
-(void)tranData:(MyBlock)block
{
    self.myblock = block;   
}

@end
Delegate传值,网上资料一大片,这里不多作阐述,贴其他网址,BD说我回答里有广告,坑。你自己搜吧。

备注:(我说的这个前后,主要根据ViewController的初始化时间点区分的)

网友(2):

Flutter 是最新的一代跨平台APP开发框架,使用Flutter可以快速构建精美的应用,在 Flutter 页面 A跳转页面B 可通过 静态路由与动态路由两种方式,有时也需要 在两个页面之间传值,本视频讲解的就是这方面