我是一个初学者,正在尝试弄清楚如何正确使用 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;
}
我应该向 viewDidLoad 方法添加什么,以便将导航栏嵌入到表格 View 中?
发送
最佳答案
不需要将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
可能是应用程序主 UIWindow
的 rootViewController
(属性 你的 AppDelegate
)。
因此,要使此设置正常工作,您必须在 AppDelegate
的 application:didFinishLaunchinWithOptions
中设置应用程序的根窗口:
- (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;
}
这会产生这样的效果,即您将在应用程序启动时看到的 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];
}
您可以在此处使用self.navigationController
,因为homeViewController
嵌入在UINavigaitonController
中,因此iOS 会为您设置此属性。
希望对您有所帮助! :)
更新:
如果您不想将 HomeViewController
嵌入到 UINavigationController
中,只需像这样修改 goToStack
:
- (IBAction)goToStack:(id)sender {
StackTableViewController *stackViewController = [[StackTableViewController alloc] init]; // shouldnt this get loaded from a NIB though???
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:stackViewController];
[self presentViewController:navigationController animated:YES completion:nil];
}
关于ios - 如何将导航 Controller 添加到不是主视图的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27443629/