ios - 如何将导航 Controller 添加到不是主视图的 View ?

标签 ios objective-c iphone uinavigationcontroller nib

我是一个初学者,正在尝试弄清楚如何正确使用 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 可能是应用程序主 UIWindowrootViewController(属性 你的 AppDelegate)。

因此,要使此设置正常工作,您必须在 AppDelegateapplication: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/

相关文章:

iphone - iOS 正向地理编码 block 未被执行

objective-c - 此应用程序的模型- View - Controller (矩阵操作)

ios - RestKit 0.20 - POST 多个对象

ios - RLMArray 到 RLMLinkingObjects 迁移

iPhone MapKit - 更新注释坐标和 map

ios - 如何在我的网络服务中显示加载 View Controller

ios - 如何从类对象列表构造一个 json 对象

ios - 试飞 : "The invite has already been used"

ios - UIpickerView 在同一 View Controller 中填充多个 uitextFields

ios - 在 iOS 中使用 Swift 数据库