如何在iphone中使用一个参数从另一个全局函数调用全局函数?

时间:2012-06-22 07:53:58

标签: iphone

HiI对iphone开发很新。我是java后台开发人员。所以我的方法就像java。

这里我的要求是我想从每个视图控制器调用一个全局函数。 从那里我传递self.navigationController作为参数。在该功能中,我添加了一些按钮。当用户点击该按钮时,它应该再调用一个函数,它应该携带相同的对象作为参数。

请给我指导。我尝试如下,但它在编译时显示错误

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];

请告诉我如何实现这个目标

4 个答案:

答案 0 :(得分:2)

在app delegate中添加一个函数。并在所有视图控制器中调用该函数。使用appdelegate.then的对象在该函数中添加一个带有id sender的按钮,并在方法中做你想要的watever。

答案 1 :(得分: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];

答案 2 :(得分: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];

这可能会对你有所帮助

答案 3 :(得分: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];

}