3

我是iOS新手,我想在我的视图控制器上添加一个导航栏,左侧有2个按钮,右侧订阅。我不知道该怎么做..直到现在我刚刚从界面生成器中添加了一个导航栏,在 .h 文件中为它创建了一个(强)参考并进行了以下编码。

 navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 1026, 50)];
[navBar setTintColor:[UIColor clearColor]];
[navBar setBackgroundColor:[UIColor redColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"subscribe" style:UIBarButtonItemStyleBordered target:self action:@selector(editBotton)];

bi1.style = UIBarButtonItemStyleBordered;

bi1.tintColor =[UIColor colorWithWhite:0.305f alpha:0.0f];

self.navigationItem.rightBarButtonItem = bi1;

但什么都没有发生..请帮忙

4

2 回答 2

8

您可以在 AppDelegate 中添加,

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
                                                                 bundle: nil];
     
        SampleViewController *mainViewController = (SampleViewController*)[mainStoryboard
                                                           instantiateViewControllerWithIdentifier: @"SampleViewController"];
     
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
     
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.window setRootViewController:navigationController];
        [self.window setBackgroundColor:[UIColor whiteColor]];
        [self.window makeKeyAndVisible];
     
        return YES;
    }
于 2013-10-11T11:27:37.763 回答
2

我认为您必须学习有关UINavigationController的基本概念。您可以从以下教程中学习:

http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/

http://www.ralfebert.de/archive/ios/tutorial_iosdev/navigationcontroller/

http://bharanijayasuri.wordpress.com/2012/12/19/simple-uinavigationcontroller-tutorial-2/

于 2013-10-11T11:20:47.767 回答