简体   繁体   English

iOS:将操作栏按钮委托给其他类

[英]iOS: Delegate action bar button to other class

I have a UIViewController with a Container, and I have a UIBarButton on Navigation Bar, and I want the action of button go to the other class, the class of View1. 我有一个带有容器的UIViewController,并且在导航栏上有一个UIBarButton,并且我希望按钮的动作进入另一个类,即View1的类。

The class for the "Embed Segue" I don´t show because it is not need. 我没有显示“嵌入Segue”的课程,因为它不是必需的。

When I click the button nothing happens, not even the information in log (NSLog). 当我单击按钮时,什么都没有发生,甚至日志(NSLog)中的信息也没有。

someone help me? 谁来帮帮我?

I have this: 我有这个:

//  ViewController.h
#import <UIKit/UIKit.h>

@protocol changeDelegate <NSObject>
-(void) changeLabel;
@end

@interface ViewController : UIViewController

@property (weak, nonatomic) id <changeDelegate> delegate;

@end


//  ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)changeButton:(UIBarButtonItem *)sender {

    [self.delegate changeLabel];

}

@end
//  Container.h

#import <UIKit/UIKit.h>

@interface Container : UIViewController

@end


//  Container.m

#import "Container.h"

@interface Container ()

@end

@implementation Container

- (void)viewDidLoad {
    [super viewDidLoad];

    [self performSegueWithIdentifier:@"segueView1" sender:nil];

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    [self addChildViewController:segue.destinationViewController];
    [self.view addSubview:((UIViewController *)segue.destinationViewController).view];

}

@end
//  View1.h

#import <UIKit/UIKit.h>

@interface View1 : UIViewController <changeDelegate>

@end


//  View1.m

#import "View1.h"

@interface View1 ()

@property (weak, nonatomic) IBOutlet UILabel *labelView1;

@end

@implementation View1

- (void)viewDidLoad {
    [super viewDidLoad];

}

-(void) changeLabel {
    NSLog(@"Button Action !");
    self.labelView1.text = @"Button Action !";
}

@end

Thanks, JProveta 谢谢,JProveta

I guess the problem is your delegate method is not firing. 我想问题是您的委托方法没有触发。 Try adding this in viewdidload of view.m 尝试将其添加到view.m的viewdidload中

Viewcontroller *obj  = [[Viewcontroller alloc]init];
obj.delegate = self;

You see there's no call -(void) changeLabel , meanwhile ViewController don't know who it's delegate. 您会看到没有调用-(void) changeLabel ,同时ViewController不知道它是谁的委托。 You should assign the delegate for ViewController, in View1.m , you should have something like; 您应该为ViewController分配委托,在View1.m ,您应该有类似以下内容:

ViewController *vC = [ViewController new];
vC.delegate = self; // changeDelegate

Then 然后

[self.delegate changeLabel];

is something like: [view1 changeLabel]; 类似于: [view1 changeLabel]; then the changeLabel was called. 然后调用changeLabel。


For your project: 对于您的项目:

"ViewController.h" - (IBAction)changeButton:(UIBarButtonItem *)sender { “ ViewController.h” -(IBAction)changeButton:(UIBarButtonItem *)sender {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"change_label_notfication" object:nil];

    //[self.delegate changeLabel];
    NSLog(@"button");

}

"View1.h" “View1.h”

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabel) name:@"change_label_notfication" object:nil];
}

Some suggestions: 一些建议:

  1. ViewController's is controller of a view, so @interface View1 : UIViewController <changeDelegate> View1 is not a good name. ViewController是视图的控制器,因此@interface View1 : UIViewController <changeDelegate> View1不是一个好名字。 2.Not sure why you do in this way? 2.不确定为什么要这样做吗? But it's really a not good structure for you code. 但这对于您的代码来说确实不是一个很好的结构。 ViewController.h can set a label can controller properties, "Container.h" make no scene in your point. ViewController.h可以设置标签可以控制器属性,“ Container.h”在您的视线中不会出现任何场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM