2012-06-22 104 views
1

嗨,我很新的iphone開發。我是java背景developer.So我的方法就像java。如何在iPhone中使用一個參數從另一個全局函數調用全局函數?

這裏我的要求是,我想從每個視圖控制器調用一個全局函數。 並從那裏我傳遞self.navigationController作爲參數。在那個函數中,我添加了一些buton。當用戶點擊該按鈕時,它應該調用一個更多的功能,它應該攜帶與參數相同的對象。

請給我指導。我嘗試如下,但它正顯示出在編譯時

Utilities.m

 +(void)setBacKButton:(UINavigationController *)navigationController 
    { 

     for(UIButton *btn in navigationController.navigationBar.subviews){ 
      if([btn isKindOfClass:[UIButton class]]){ 
       [btn removeFromSuperview]; 
      } 
     } 

     UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom]; 
     btn2.frame=CGRectMake(708, 0, 50, 54); 
     [btn2 setImage:[UIImage imageNamed:@"btn_back.png"] forState:0]; 
     [btn2 setImage:[UIImage imageNamed:@"btn_back_h.png"] forState:UIControlStateSelected]; 
     // here i need to call bellow function when click on this button 
//[btn2 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside]; 
     [navigationController.navigationBar addSubview:btn2]; 
    } 
    +(void)gotoBack:(UINavigationController *)navigationController 
    { 
     [navigationController popViewControllerAnimated:YES]; 
    } 

錯誤,我調用該函數從我的ViewController作爲

[Utilities setBacKButton:self.navigationController]; 

請告訴我如何才能做到這一點

回答

1

只是做一個導航控制器在你的應用程序委託...並推動你的視圖控制器使用此..按照下面的代碼...

//YourAppdelegate.h 
    UINavigationController *navController; 
    @property(nonatomic,retain)UINavigationController *navController; 

    // your appdelegate.m 

    navController = [UINavigationController alloc]initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navController ; 

,現在使你的全局函數這裏(appdelegate.m)像.......

- (void)setBacKButton:(UINavigationController *)navigationController{ 
    // put your code here 
    } 

現在從那裏你有你的視圖控制器叫它需要像這樣...

// yourViewController.m 

進口「yourAppdelegate.h」

,當你要調用該函數只寫這兩條線..

YourAppdelegate *appdelegate = (YourAppdelegate*)[[UIApplication sharedApplication]delegate]; 

    [appdelegate setBacKButton:self.navigationController]; 

可能,這將幫助你

2

在您的應用程序中添加一個函數delegate.and在所有視圖控制器中調用該函數。使用appdelegate.then的對象在該函數中添加一個帶有ID sen你需要的方法。

+0

謝謝你的重播,但我想實現一種方法來彈出屏幕。爲此,我需要navigationcontroler對象,所以我如何獲得導航控制器對象? –

+0

只是在您的appdelegate中初始化一個導航控制器對象,並將您的視圖控制器推送到該導航控制器。這是作爲基於導航的項目的基本原理。因此,您只需要在該功能中使用視圖控制器對象並將其推送到appdelegate導航控制器。 – hacker

1

你想要創建一個UINavigationBar類別。

UINavigationBar的+ customBackButton.h

#import <UIKit/UIKit.h> 

@interface UINavigationBar (customBackButton) 
- (void)setCustomBackButton; 
@end 

UINavigationBar的+ customBackButton.m

#import "UINavigationBar+customBackButton.h" 

@implementation UINavigationBar (customBackButton) 

- (void)setCustomBackButton 
{ 
    for(UIButton *btn in self.subviews) 
    { 
     if([btn isKindOfClass:[UIButton class]]) 
     { 
      [btn removeFromSuperview]; 
     } 
    } 

    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom]; 
    btn2.frame=CGRectMake(708, 0, 50, 54); 
    [btn2 setImage:[UIImage imageNamed:@"btn_back.png"] forState:0]; 
    [btn2 setImage:[UIImage imageNamed:@"btn_back_h.png"] forState:UIControlStateSelected]; 
    // here i need to call bellow function when click on this button 
    //[btn2 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:btn2]; 
} 

@end 

然後在任意視圖控制器,你可以導入像這樣

#import "UINavigationBar+customBackButton.h" 

[self.navigationController.navigationBar setCustomBackButton]; 
+0

當按鈕被點擊時,這種方法如何實際回到前一個視圖? – dbau

0

在從@ewiinnnnn上面的響應,我想出了一個辦法,用自定義類別,並有後退按鈕導航預期:

// UIViewController+customBackButton.h 

#import <UIKit/UIKit.h> 

@interface UIViewController (customBackButton) 
- (void)setCustomBackButton; 
@end 


// UIViewController+customBackButton.m 

#import "UIViewController+customBackButton.h" 

@implementation UIViewController (customBackButton) 


// sets my custom back button 
- (void)setCustomBackButton 
{ 

    UINavigationItem *navItem = self.navigationItem; 

    UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    UIImage *addButton = [UIImage imageNamed:@"backButton.png"]; 
    UIImage *addButtonSelected = [UIImage imageNamed:@"backButtonHighlighted.png"]; 

    [customButton setBackgroundImage:addButton forState:UIControlStateNormal]; 
    [customButton setBackgroundImage:addButtonSelected forState:UIControlStateHighlighted]; 
    customButton.frame=CGRectMake(0.0, 0.0, addButton.size.width, addButton.size.height); 
    [customButton addTarget:self action:@selector(navigateBack) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView: customButton]; 


    navItem.leftBarButtonItem = barButtonItem; 


} 

// the action that is triggered when the back button is pressed 
- (void)navigateBack { 
    [self.navigationController popViewControllerAnimated:YES]; 
} 


@end 

然後在一個UIViewController內:

// MyViewController.m 

#import "UIViewController+customBackButton.h" 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // add back button to navcontroller 
    [self setCustomBackButton]; 

} 
相關問題