如何将导航控制器添加到不是主视图的视图?

时间:2014-12-12 12:24:49

标签: ios objective-c iphone uinavigationcontroller nib

我是初学者,并试图弄清楚如何正确使用笔尖。

我有一个HomeViewController.h:

#import <UIKit/UIKit.h>
#import "StackTableViewController.h"

@interface HomeViewController : UIViewController

@property (strong, nonatomic) StackTableViewController *stackViewController;

- (IBAction)goToStack:(id)sender;


@end

HomeViewController.m:

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)init {

    self = [super initWithNibName:@"HomeViewController" bundle:nil];
    if (self) {
        //
        _stackViewController = [[StackTableViewController alloc]init];
    }

    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goToStack:(id)sender {

    //[self.navigationController showViewController:_stackViewController sender:self];
    [self presentViewController:_stackViewController animated:YES completion:nil];
}

正如您所看到的,我正在从HomeViewController建模到StackTableViewController ...

现在它工作正常,但我希望StackTableViewController将嵌入到NavigationController中...我可以在顶部放置一个取消按钮。

这是我的StackTableViewController.m:

#import "StackTableViewController.h"

@interface StackTableViewController ()

@property (strong, nonatomic) UINavigationController *navBar;

@end

@implementation StackTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}

我应该将什么添加到将在表视图中嵌入navBar的viewDidLoad方法?

tnx

1 个答案:

答案 0 :(得分:1)

无需将StackTableViewController声明为HomeViewController的属性,只需修改goToStack,如下所示:

- (IBAction)goToStack:(id)sender {
     StackTableViewController *stackViewController = [[StackTableViewController alloc] init]; // shouldnt this get loaded from a NIB though???
    [self presentViewController:stackViewController animated:YES completion:nil];
}

关于UINavigationController的问题,根据您的设置,UINavigationController是应用中特定导航堆栈的基础,因此,如果您只是构建一个没有标签栏的简单应用或者另一个更复杂的界面,您的UINavigationController可能是应用程序主rootViewController的{​​{1}}(UIWindow属性)。

因此,要使此设置生效,您需要做的是AppDelegate的{​​{1}},设置应用程序的根窗口:

application:didFinishLaunchinWithOptions

这将导致您在应用启动时看到的AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; // I assume you have a NIB file called HomeViewController UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController]; self.window.rootViewController = navigationController; return YES; } 的实例中嵌入,这又是{{1}你的整个申请。

然后,您可以修改HomeViewController以使用此UINavigationController 的实例,而不是以模式显示rootViewController

goToStack

您可以在此使用UINavigationController,因为stackViewController中嵌入了- (IBAction)goToStack:(id)sender { StackTableViewController *stackViewController = [[StackTableViewController alloc] init]; // shouldnt this get loaded from a NIB though??? [self.navigationController pushViewController:stackViewController animated:YES]; } ,因此iOS会为您设置此属性。

希望有所帮助! :)

<强>更新: 如果您不想在self.navigationController中嵌入homeViewController,请修改UINavigaitonController,如下所示:

HomeViewController