2012-12-10 80 views
2

這是我的代碼..我無法在我的導航控制器中添加按鈕。如何將按鈕添加到導航控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    UIViewController *viewController1, *viewController2, *viewController3,*viewController4; 

    viewController1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    viewController2 = [[DisplayAllImagesController alloc] initWithNibName:@"DisplayAllImagesController" bundle:nil]; 
    viewController3 = [[EmptyView alloc] initWithNibName:@"EmptyView" bundle:nil]; 
    viewController4 = [[ListView alloc] initWithNibName:@"ListView" bundle:nil]; 



    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController2, viewController1, viewController3, viewController4, nil]; 
    self.tabBarController.title = @"Title"; 

    self.navController = [[UINavigationController alloc] 
          initWithRootViewController:self.tabBarController]; 
    self.window.rootViewController = self.navController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

所以,請讓我知道我可以添加按鈕,導航控制器......求你......

+0

你想leftbutton是分辯按鈕? –

+0

您要添加哪個按鈕?ieback按鈕,添加按鈕等 –

+0

@ganesh manoj rightbutton .. – Shivaay

回答

5

只是添加此婁代碼在你ViewControllerviewDidLoad:方法...

UIBarButtonItem *btnReload = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(btnReloadPressed:)]; 
self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnReload; 
btnReload.enabled=TRUE; 
btnReload.style=UIBarButtonSystemItemRefresh; 
3

試試這個:

UIButton *customButton = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 50,30)]; 

UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:customButton]; 
self.navigationItem.rightBarButtonItem = closeButton; 
+0

... tnx ... fr回答bt不工作... – Shivaay

+0

您需要將該按鈕初始化爲「 UIBarButtonItem「... –

+0

tnx ..bt仍然工作着我... – Shivaay

2

,如果你想自定義按鈕,我們可以使用這

UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton1 setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; 
myButton1.showsTouchWhenHighlighted = YES; 
myButton1.frame = CGRectMake(0.0, 3.0, 50,30); 

[myButton1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1]; 
self.navigationItem. rightBarButtonItem = rightButton; 
0

UIButton * btn = [UIButton alloc] init]; .... self.navController.view addsubview:btn];

1

//創建按鈕,指定圖像 的UIImage *按鈕畫面= [UIImage的imageNamed:@ 「button.png」]; UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];其中,UIButton * UButton = [UIButton buttonWithType:UIButtonTypeCustom];其中,UIButton *

[rightButton setFrame:CGRectMake(280,7,buttonImage.size.width,buttonImage.size.height)]; 
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
rightButton.titleLabel.font = [UIFont boldSystemFontOfSize:12]; 
[rightButton setTitle:@"Right" forState:UIControlStateNormal]; 

// add image view 
[self.navigationController.navigationBar addSubview:rightButton]; 

[self.navigationController.navigationBar bringSubviewToFront:rightButton]; 


//create a UIBarButtonItem with the button as a custom view 
UIBarButtonItem *customBarDone = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; 
self.navigationItem.rightBarButtonItem = customBarDone; 
[rightButton addTarget:self action:@selector(yourSelector:) forControlEvents:UIControlEventTouchUpInside]; 
+0

這是工作代碼,只需將自定義圖像分配給按鈕...它將適用於您自己的場景... – nsgulliver

相關問題