Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在具有内容视图和视图控制器的tabbarcontroller之间传递数据_Ios_Uiviewcontroller_Uitabbarcontroller - Fatal编程技术网

Ios 在具有内容视图和视图控制器的tabbarcontroller之间传递数据

Ios 在具有内容视图和视图控制器的tabbarcontroller之间传递数据,ios,uiviewcontroller,uitabbarcontroller,Ios,Uiviewcontroller,Uitabbarcontroller,我使用的是xib而不是故事板 AppDelegate.m ViewController1 *ViewController1 = [[ViewController1 alloc] init]; ViewController1.title = NSLocalizedString(@"1", @"1"); ViewController2 *ViewController2 = [[ViewController2 alloc] init]; ViewController2.title = NSLocali

我使用的是xib而不是故事板

AppDelegate.m
ViewController1 *ViewController1 = [[ViewController1 alloc] init];
ViewController1.title = NSLocalizedString(@"1", @"1");
ViewController2 *ViewController2 = [[ViewController2 alloc] init];
ViewController2.title = NSLocalizedString(@"2", @"2");

BannerViewController *_bannerViewController1;
_bannerViewController1 = [[BannerViewController alloc] initWithContentViewController:ViewController1];

BannerViewController *_bannerViewController2;
_bannerViewController2 = [[BannerViewController alloc] initWithContentViewController:ViewController2];

_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = @[_bannerViewController1,_bannerViewController2];

self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];


ViewController1.m
#import "ViewController1.h"
#import "ViewController2.h"

ViewController2 *cvc = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
cvc.strReceiveValue= "Testing";

ViewController2.h
@property (strong, retain) NSString *strReceiveValue;
ViewController2.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"strMortgageAmount: %@", strMortgageAmount);
知道我为什么没有得到这个值吗?我想这可能与initWithContentViewController有关。

**

请参阅下面的更新 **

如果我理解正确,您是在AppDelegate中创建选项卡栏控制器吗?是否要将信息传递给与选项卡栏控制器关联的视图控制器

如果是,请看以下内容:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController setDelegate:self];
UINavigationController *dashTVCnav = [[tabBarController viewControllers] objectAtIndex:0];
然后:

然后在DashTVC.h中:

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSString *uuid;
这样,就可以将managedObjectContect和uuid传递给dashTVC控制器

我希望我能理解你的要求

+++++++++++++++更新+++++++++++++++++++++

原始海报很好,给了我他的代码样本。我将把它的一部分和我的补丁发布在这里,供将来通过搜索找到它的人使用

有几个问题,但简而言之,没有向vc2传递任何信息:

首先,没有从appDelegate传递任何内容

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect bounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:bounds];
self.window.backgroundColor = [UIColor whiteColor];

ViewController1 *vc1 = [[ViewController1 alloc] init];
vc1.title = NSLocalizedString(@"VC1", @"VC1");

ViewController2 *vc2 = [[ViewController2 alloc] init];
vc2.title = NSLocalizedString(@"VC2", @"VC2");

//---  I added this as a test
vc2.strReceiveValue=@"foo";

// and now foo shows up in the debug console when vc2 is fixed




_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = @[
    [[BannerViewController alloc] initWithContentViewController:vc1],
    [[BannerViewController alloc] initWithContentViewController:vc2],
];

self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

接下来,有必要修改ViewWillDisplay在viewController2.m中

- (void)viewWillAppear:(BOOL)animated {

// line below missing
[super viewWillAppear:animated];
NSLog(@"output: %@", strReceiveValue);
}


希望这对您有所帮助,并享受为IOS开发的乐趣

这是一个很好的答案,以审查以及我已经更新了问题,我认为现在应该清楚地解释问题。。。希望如此。首先,您显然已经编辑了一堆代码,这很好,尽管可能还有其他问题,但这是从哪里来的?NSLog@strMortgageAmount:%@,strMortgageAmount;NSString属性是*strReceiveValue;这是一个打字错误:NSLog@strReceiveValue:%@,strReceiveValue;让我创建示例并在dropbox中共享。。我是iOS新手,但我真的很喜欢玩它。。。谢谢你的帮助。。
- (void)viewWillAppear:(BOOL)animated {

// line below missing
[super viewWillAppear:animated];
NSLog(@"output: %@", strReceiveValue);