我不知道 Monotouch,但这是我在 Objective-c 中的做法。我没有找到关于这个话题的任何东西,所以如果有什么问题,请大家评论:) 顺便说一句,我使用的是 ARC,所以我不手动管理内存!我需要实现的就像你一样,有一个标签栏,加载相同的 viewController,但为每个标签加载不同的数据。
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *root = (UITabBarController*)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
TeamViewController *home = [[mainStoryboard instantiateViewControllerWithIdentifier:@"Team"] initHome];
TeamViewController *visitor = [[mainStoryboard instantiateViewControllerWithIdentifier:@"Team"] initVisitor];
[root setViewControllers:[NSArray arrayWithObjects:home, visitor, nil] animated:NO];
UITabBar *tabs = root.tabBar;
UITabBarItem *homeTab = [tabs.items objectAtIndex:0];
UITabBarItem *visitorTab = [tabs.items objectAtIndex:1];
homeTab.title = @"Home team";
visitorTab.title = @"Visitor team";
return YES;
}
你可以看到我打电话initHome
,initVisitor
当我加载我的两个时TeamViewController
,这里是关于它的代码。
TeamViewController.h
@interface TeamViewController : UIViewController
{
enum
{
HOME,
VISITOR
};
int team;
}
TeamViewController.m
- (id)initHome
{
team = HOME;
return self;
}
- (id)initVisitor
{
team = VISITOR;
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(team == HOME)
{
label.text = @"home data";
}
else if(team == VISITOR)
{
label.text = @"visitor data";
}
}
我不知道你能把它翻译成你的项目有多好,但我希望你能全面了解它:)
如果您需要阅读有关如何使用情节提要访问第一个视图控制器的信息:http: //developer.apple.com/library/ios/#releasenotes/Miscellaneous/RN-AdoptingStoryboards/_index.html#//apple_ref/ doc/uid/TP40011297
有一段叫做“访问第一个视图控制器”