1

您好我对 iphone 开发非常陌生。我是 java 背景开发人员。所以我的方法就像 java 。

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

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

实用程序.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];
    }

我从我的视图控制器中调用该函数为

[Utilities setBacKButton:self.navigationController];

请告诉我我们如何实现这一目标

4

4 回答 4

2

在您的应用程序委托中添加一个函数。并在您的所有视图控制器中调用该函数。使用 appdelegate 对象。然后在该函数中添加一个带有 id sender 的按钮,并在方法中执行您想要的操作。

于 2012-06-22T07:58:28.277 回答
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];
于 2012-06-22T08:29:30.467 回答
1

只需在您的应用程序中创建一个导航控制器 Delegate ...并使用此推送您的视图控制器..按照以下代码...

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

这可能会帮助你

于 2012-06-22T09:59:36.123 回答
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];

}
于 2013-06-23T12:17:55.557 回答