将标题+按钮添加到UINavigationController

时间:2012-04-07 10:18:05

标签: iphone uinavigationcontroller

我为解决这个问题而疯狂。

我的AppDelegate.m中有以下代码 基本上是导航控制器+标签栏,链接到2 tableviewcontroller

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    ElencoSpese *elenco=[[ElencoSpese alloc] init];
    elenco.tabBarItem.title=@"Prova 123";

    ElencoSpese *elenco2=[[ElencoSpese alloc] init];
    elenco2.tabBarItem.title=@"Prova 222";

    UITabBarController *tabMenu = [[UITabBarController alloc] init];
    tabMenu.viewControllers=[NSArray arrayWithObjects:elenco, elenco2, nil];


    UINavigationController *navig=[[UINavigationController alloc] initWithRootViewController:tabMenu];
    self.window.rootViewController=navig;

    [self.window makeKeyAndVisible];

    return YES;
}

ElencoSpeseTableViewController,它可以正常使用。我想在此窗口中添加"title",在"Edit" ....

上添加“+”和NavigationBar按钮

我试图在tableviewcontroller的loadview方法中取消注释以下内容:no result

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;

我已经尝试在appdelegate设置标题....没有...

3 个答案:

答案 0 :(得分:1)

这样做..

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    ElencoSpese *elenco=[[ElencoSpese alloc] init];
    elenco.tabBarItem.title=@"Prova 123";

    ElencoSpese *elenco2=[[ElencoSpese alloc] init];
    elenco2.tabBarItem.title=@"Prova 222";
    UINavigationController *navig=[[UINavigationController alloc]     initWithRootViewController:elenco];
UINavigationController *navig1 =[[UINavigationController alloc] initWithRootViewController:elenco2];

    UITabBarController *tabMenu = [[UITabBarController alloc] init];
    tabMenu.viewControllers=[NSArray arrayWithObjects:navig, navig1, nil];

    self.window.rootViewController=tabMenu;

    [self.window makeKeyAndVisible];

    return YES;
}

然后每个 TableViewController -viewDidLoad 添加如下

-(void)viewDidLoad
{
   [super viewDidLoad];
   self.title = @"Calculator"; 
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(yourSelectorMethod)];

    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(yourSelectorMethod1)];

    self.navigationItem.leftBarButtonItem = item;
    self.navigationItem.rightBarButtonItem = item1;
}

答案 1 :(得分:0)

viewcontroller嵌入在tabbar-controller中,该控制器在navigationcontroller中被推送。所以你应该像这样访问navigationitem:

self.tabBarController.navigationItem.rightBarButtonItem = self.editButtonItem;

答案 2 :(得分:0)

窗口的标题可以如下给出

在代码中的行之后

self.window.rootViewController=navig;

只需添加

self.tabbarController.navigationItem.title = @"YOUR TITLE";

这将帮助您提供标题