我已经按照几个关于在视图控制器之间传递数据的指南,但它们似乎涉及在任何等效的 - (void)goToNextView方法中设置secondViewController的属性。我正在尝试弄清楚如何使用在app delegate中创建的标签栏控制器,从第一个视图控制器中为属性分配值。
当前设置:
AppDelegate.m
@synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
...
UITabBarController *tbc = [[UITabBarController alloc] init];
FirstViewController *vc1 = [[FirstViewController alloc] init];
SecondViewController *vc2 = [[SecondViewController alloc] init];
[tbc setViewControllers: [NSArray arrayWithObjects: vc1, vc2, nil]];
[[self window] setRootViewController:tbc];
[self.window makeKeyAndVisible];
return YES;
}
...
@end
FirstViewController.h
#import "SecondViewController.h"
@interface FirstViewController : UIViewController
{
SecondViewController *vc2;
IBOutlet UISlider *sizeSlider;
}
@property (nonatomic, retain) SecondViewController *vc2;
@property (strong, nonatomic) UISlider *mySlider;
-(IBAction) mySliderAction:(id)sender;
@end
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation FirstViewController
@synthesize vc2, mySlider;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"xib"];
}
return self;
}
...
- (IBAction) mySliderAction:(id)sender
{
NSString *str = [[NSString alloc] initWithFormat:@"%3.2f", mySlider.value];
if(self.vc2 == nil)
{
SecondViewController *viewTwo = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.vc2 = viewTwo;
}
vc2.sliderValueString = str;
}
...
@end
SecondViewController.h
#import <UIKit/UIKit.h>
#import "myView.h"
@interface SecondViewController : UIViewController
{
NSString *sliderValueString;
}
@property (copy) NSString *sliderValueString;
@end
SecondViewController.m
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize sliderValueString;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"View 2"];
}
return self;
}
-(void) loadView
{
CGRect frame = [[UIScreen mainScreen] bounds];
myView *view = [[myView alloc] initWithFrame:frame];
printf("Slider Value: %s", [sliderValueString UTF8String]);
[self setView: view];
}
...
@end
myView.h和.m可能与这个问题无关,但是我有.h子类化UIView,而.m用 - (id)initWithFrame:(CGRect)框架创建视图
我猜我将第二个VC的属性分配到了错误的地方(mySliderAction),因为第二个VC中的printf()是空白的,所以我的问题是:应该在哪里分配属性,或者我做错了什么阻止第二个VC允许它的sliderValueString不被(或保持)分配?
谢谢!
答案 0 :(得分:0)
您可以在mySliderAction
中设置属性,但问题是您将其设置在错误的对象上。
在App Delegate中,为tabControllers的第2项创建一个viewController:
SecondViewController *vc2 = [[SecondViewController alloc] init];
在vc1的mySliderAction
中你也创建了一个vc2的实例:
SecondViewController *viewTwo = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
这不是您通过tabController上的第二个选项卡导航到的实例,但它是您正在设置的sliderValueString
属性的实例。
您需要引用已存在的实例,而不是在此处创建新实例:
SecondViewController *viewTwo = [self.tabBarController.viewControllers objectAtIndex:1]
然后你应该没事。