ios - 我们如何在自定义 UIView 类中调用操作?需要在所有 View Controller 中重用 UIView 类

标签 ios objective-c uiview uiviewcontroller uibutton

我创建了一个自定义 UIView 类。每当调用 backButton 时,我都想调用一个 Action 。需要在所有 View Controller 中重用此 UIView 类以及操作。我可以在 UIView 类本身或 View Controller 中编写吗?

#import "CustomView.h"

@implementation CustomView

- (instancetype)init {

   if(self = [super init]){
    //-------------------custom headerView----------------
    self.headerView = [[UIView alloc]init];
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    self.headerView.backgroundColor = [UIColor redColor];
    [self addSubview:self.headerView];

    self.backButton = [[UIButton alloc]initWithFrame:CGRectMake(5,13, 12, 16)];
    [self.backButton setImage:[UIImage imageNamed:@"back-btn.png"]
                     forState:UIControlStateNormal];
    [self.backButton addTarget:self
                        action:@selector(backButtonAction)
              forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.backButton];
   }
   return self;
}

后退按钮 Action
-(void)backButtonAction{
   [[self navigationController] popViewControllerAnimated:YES];
}

最佳答案

这很简单
您必须为 UIViewController 创建类别喜欢 UIViewController+Additions

  • 点击File -> New -> File
  • 点击Objective-C fileSources 下在 iOS然后点击下一步
  • 现在下 File Type:选择Category
  • 然后设置文件名:Additions并设置类:UIViewController


  • UIViewController+Additions.h
    首先在.h中创建你的后退按钮功能>>
    -(void)addBackButton;
    

    UIViewController+Additions.m
    -(void)addBackButton
    {
        self.navigationItem.leftBarButtonItem = nil;
        UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       // [aBtn setTitle:@"Back" forState:UIControlStateNormal];
        [aBtn setImage:[UIImage imageNamed:@"back-btn.png"] forState:UIControlStateNormal];
        [aBtn setFrame:CGRectMake(0, 0,50, 64)];
        [aBtn.titleLabel setFont:[UIFont systemFontOfSize:18.0]];
        aBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        aBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        aBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
        [aBtn setTitleColor:[UIColor colorWithRed:1/255.0 green:65/255.0 blue:96/255.0 alpha:1.0] forState:UIControlStateNormal];
    
            [aBtn addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
        UIBarButtonItem *aBarBtn = [[UIBarButtonItem alloc]initWithCustomView:aBtn];
        self.navigationItem.leftBarButtonItem = aBarBtn;
    }
    
    -(void)backButtonAction
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    像这样使用:

    只需在任何 View Controller 类中导入类别 #import "UIViewController+Additions.h"
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
         [self addBackButton];
    }
    

    关于ios - 我们如何在自定义 UIView 类中调用操作?需要在所有 View Controller 中重用 UIView 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36262168/

    相关文章:

    objective-c - 无法确定设备的当前方向

    javascript - 使用 React-Native AsyncStorage 通过应用程序更新在 App Store/Google Play 中存储数据

    ios - 我在哪里可以找到 "documentary evidence"以获得 App Store Approval 以下载 PUBLIC Facebook 视频?

    ios - 调用什么方法来处理 UIObject 的事件?

    objective-c - Cocoa:使用 NSSet 作为 NSMutableDictionary 中的键有什么缺点吗?

    ios - ip camera feed 到 IOS 应用程序如何允许全局连接?

    iphone - 在制作动画时应用 CAAnimation 的更改

    ios - CALayer subLayer 边界舍入误差?

    ios - 使用自定义过滤器以图像形状显示自定义相机 ios

    ios - 如何自动调整 ViewController Root View 的大小以适应其内容?