这是我的代码,它在我发送Label值时有效,但当我传递为字符串时显示为null请帮助我... 建议我将一个String传递给第二个视图
这是我的第一个vc“AssignmentsDetailsJson.j”
#import <UIKit/UIKit.h>
#import "DocumentsViewController.h"
@interface AssignmentsDetailsJson : <UITabBarControllerDelegate,UITabBarDelegate>
@end
这是AssignmentsDetailsJson.m文件
#import "AssignmentsDetailsJson.h"
@interface AssignmentsDetailsJson ()
@property(strong, nonatomic) DocumentsViewController *controllerB;
@end
@implementation AssignmentsDetailsJson
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
self.controllerB = (DocumentsViewController *) [tabBarController.viewControllers objectAtIndex:1];
self.controllerB.data= @"yooooooooooo!";
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self;
}
这是第二个视图DocumentsViewController.h
#import <UIKit/UIKit.h>
#import "AssignmentsDetailsJson.h"
@interface DocumentsViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *aLabelInControllerB;
@property (nonatomic, strong) NSString *data;
@property(strong, nonatomic) DocumentsViewController *controllerB;
@end
这是DocumentsViewController.m
#import "DocumentsViewController.h"
@interface DocumentsViewController ()
@end
@implementation DocumentsViewController
@synthesize data;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"hooo %@",data);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
答案 0 :(得分:0)
您可以使用delegate或NSNotification执行此操作,风险高于delegate
。
答案 1 :(得分:0)
问题是在传递数据之前会调用viewDidLoad。这就是你在NSLog语句中没有得到任何输出的原因。而不是tabBarControllerDidSelect [...]尝试以下内容:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[SecondViewController class]]) {
((SecondViewController *)viewController).data = @"passed some data";
}
return YES;
}