从视图控制器调用的自定义类中的IBAction的大问题

时间:2014-12-04 15:37:55

标签: ios objective-c ibaction

我有以下代码,我已经放在一起因为我不理解我得到的日志。我最初有一个nib文件,我从其中一个操作连接到其中一个按钮。代码永远不会改变视图,因为我尝试,所以我尝试通过标签连接按钮,只是添加一个方法来点击事件。现在请注意,这两种方法是相同的,区别仅在于它们的连接方式(两种方法都被调用)。但是在IBAction方法中,sunPushed currentVC是NULL(下面发布的日志,但是你明白了),即使它的等效常规方法正确地保存了引用。有人可以对这个问题有所了解,它整天都在困扰着我,我无法弄清楚为什么会这样。任何帮助是极大的赞赏! (我一直在想,编译器如何处理IBAction与reg。方法有所不同)

headerfile:

#import <UIKit/UIKit.h>

@interface FooterViewTest1 : UIView
//@property (strong, nonatomic) UIButton *button1;
@property (strong, nonatomic) UIViewController *currentVC;
@property (strong, nonatomic) IBOutlet UIButton *button1;

-(void)buttonPushed;
-(void)initButtons:(UIViewController *)viewController;
@end

实施

#import "FooterViewTest1.h"
#import "StartViewController.h"

@implementation FooterViewTest1

-(void)initButtons:(UIViewController *)viewController{

/*UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
[button1 setImage:[UIImage imageNamed:@"menu_bar_sun_black"] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonPushed) forControlEvents:UIControlEventTouchUpInside];
[viewController.view addSubview:button1];*/
self.currentVC = viewController;

NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"RaymioFooterView" owner:nil options:nil];
UIView *footerView = [viewController.view viewWithTag:1];
UIView *footerViewContent = [nibContents lastObject];
NSLog(@"SELFinside initbuttons%@", self);
NSLog(@"currentVCinside initbuttons%@", self.currentVC);

UIButton *button1X = (UIButton *)[footerViewContent viewWithTag:5];
[button1X addTarget:self action:@selector(buttonPushed) forControlEvents:UIControlEventTouchUpInside];

[footerView addSubview:footerViewContent];
}

- (IBAction)sunPushed{
NSLog(@"SELFinside sunpushed%@", self);
NSLog(@"currentVCinside sunpushed%@", self.currentVC);
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
StartViewController *startViewController = [storyboard instantiateViewControllerWithIdentifier:@"StartViewController"];
[self.currentVC presentViewController:startViewController animated:YES completion:nil];
}

-(void)buttonPushed{
NSLog(@"SELFinside buttonpushed%@", self);
NSLog(@"currentVCinside buttonpushed%@", self.currentVC);
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
StartViewController *startViewController = [storyboard instantiateViewControllerWithIdentifier:@"StartViewController"];
[self.currentVC presentViewController:startViewController animated:YES completion:nil];
}

@end

见下图

enter image description here

再次感谢!

初始化页脚视图:

 self.footerViewTest = [FooterViewTest1 new];
//self.footerViewTest.currentVC = self;
[self.footerViewTest initButtons:self];

1 个答案:

答案 0 :(得分:0)

我不确定你在做什么 - 但是在你的日志中self是两个不同的对象:地址0x的1个对象buttonPushed内的{6} 0和{ {1}}和另一个initButtons内的地址0x ... b90。你能展示全班吗?

编辑:

您的问题是正在通过Storyboard设置IBAction,并且正在通过调用sunPushed创建视图。要使用故事板中定义的操作而不是:

new

将该属性附加到storyboard中的视图。这样,您的代码中就会有相同的(单个!)实例。