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.
The class for the "Embed Segue" I don´t show because it is not need.
When I click the button nothing happens, not even the information in log (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
I guess the problem is your delegate method is not firing. Try adding this in viewdidload of view.m
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. You should assign the delegate for ViewController, in View1.m
, you should have something like;
ViewController *vC = [ViewController new];
vC.delegate = self; // changeDelegate
Then
[self.delegate changeLabel];
is something like: [view1 changeLabel];
then the changeLabel was called.
For your project:
"ViewController.h" - (IBAction)changeButton:(UIBarButtonItem *)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"change_label_notfication" object:nil];
//[self.delegate changeLabel];
NSLog(@"button");
}
"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:
@interface View1 : UIViewController <changeDelegate>
View1 is not a good name. 2.Not sure why you do in this way? 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.