尝试使用IBAction加载没有Nib文件的ViewController

时间:2009-10-02 20:44:08

标签: iphone cocoa-touch

我正在尝试从我的第一页上的按钮加载PatientListTableViewController,但没有成功。我有一个标签栏项目已经到达此页面。但是我希望从我的起始页面上的按钮访问同一页面,因为PatientListTableViewController是用户首次打开页面时在选项卡栏上看不到的选项卡项目。我该如何编码呢?

PatientListTableViewController的头文件包括:

@interface PatientListTableViewController : UITableViewController <PatientAddDelegate, NSFetchedResultsControllerDelegate, UINavigationBarDelegate, UITabBarControllerDelegate>{

提前感谢您提供任何帮助

PatientListTableViewController的头文件包括:

@interface PatientListTableViewController : UITableViewController [PatientAddDelegate, NSFetchedResultsControllerDelegate, UINavigationBarDelegate, UITabBarControllerDelegate] {

2 个答案:

答案 0 :(得分:0)

如果您已将此视图控制器设置为UITabBarController的子视图控制器,我将在UITabBarController上使用setSelectedIndex:setSelectedViewController。如果你不这样做,你就会遇到问题,因为你可能已经在两个不同的位置加载了视图控制器的视图(在标签栏下,以及手动加载它时放置它的任何地方)。

这是一些示例代码。我假设您的应用程序委托有一个实例变量myTabBarController,并且您在UITabBarController中放置了两个视图控制器:myFirstViewControllermySecondViewController。我假设您已将这两个视图控制器设置为NIB文件中标签栏控制器的子项。

- (void)buttonPressed:(id)sender {
    [myTabBarController setSelectedViewController:mySecondViewController];
    // Alternately, you could set the index rather than the actual VC:
    // [myTabBarController setSelectedIndex:1]
}

如果您的问题与加载没有NIB文件的视图控制器有关,请确保在该视图控制器的loadView方法中正确设置视图层次结构。

答案 1 :(得分:0)

在将此代码实现到myFirstViewController后,上面的解决方案对我不起作用:

导入“MyFirstViewController.h”

导入“MyAppDelegate.h”

@implementation MyFirstViewController

@synthesize tabBarController;

  • (void)buttonPressed:(id)sender {[tabBarController setSelectedViewController:patientListController]

在我的MyAppDelegate.h文件中,我有:

IBOutlet UITabBarController * tabBarController; IBOutlet MyTableViewController * patientListController; ....结束

在我的MyAppDelegate.m文件中,我有:

@implementation MyAppDelegate @synthesize patientListController;

@synthesize tabBarController; ......结束

我在这里错过了什么吗?