2012-06-22 150 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]; 

} 
相关问题