[英]iOS action:@selector issue when UIButton is added to another UIButton. Action only works where buttons intersect
[英]Custom delegate only works if directly called from UIButton selector ARC retain issue
我正在尝试使用委托方法将一个视图推到另一个视图。 该方法仅在以下代码时有效,并且不会为委托返回null: [button addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];
更改为[button addTarget:delegate action:@selector(pushViewController:) forControlEvents:UIControlEventTouchUpInside];
针对[button addTarget:delegate action:@selector(pushViewController:) forControlEvents:UIControlEventTouchUpInside];
。 否则,委托返回null。 我认为这是ARC无法在ViewController中保留ContentViewController的问题。 请帮忙!
注意:我正在使用ARC
ContentViewController
@class ContentViewController;
@protocol ContentViewControllerDelegate <NSObject>
@required
-(void)pushViewController:(UIViewController *)viewController;
@end
@interface ContentViewController : UIViewController
{
__weak id <ContentViewControllerDelegate> delegate;
}
@property (weak) __weak id <ContentViewControllerDelegate> delegate;
- (void)pushView:(UIViewController *)viewController;
@end
**ContentViewController.m**
#import "ContentViewController.h"
@implementation ContentViewController
@synthesize delegate;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 300, 250, 30);
button.backgroundColor = [UIColor grayColor];
[button setTitle:@"Test Button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)pushView:(UIViewController *)viewController
{
if([delegate respondsToSelector:@selector(pushViewController:)])
[delegate pushViewController:viewController];
NSLog(@"pushView");
}
ViewController
#import "ContentViewController.h"
@interface ViewController : UIViewController <ContentViewControllerDelegate>
{
IBOutlet UINavigationController *navigationController;
__weak IBOutlet UIButton *navigationButton;
}
@property (strong, nonatomic) IBOutlet UINavigationController *navigationController;
@property (strong, nonatomic) ContentViewController *contentViewController;
@end
#import "ViewController.h"
#import "BinViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation ViewController
@synthesize navigationController;
@synthesize contentViewController;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Add the navigationController and set the frame
self.navigationController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:navigationController.view];
// Init ContentViewController to set the delegate
contentViewController = [[ContentViewController alloc] init];
// Set the delegate
contentViewController.delegate = self;
}
#pragma mark - ContentViewControllerDelegate Methods
-(void)pushViewController:(UIViewController *)viewController
{
// This is here just to see if this instance method work ... I haven't actually sent it a view to push
BinViewController *binViewController = [[BinViewController alloc] init];
[self.navigationController pushViewController:binViewController animated:YES];
}
编辑:此图像应有助于解释视图层次结构以及我在这里所做的事情。
最终使用NSNotificationcenter而不是自定义委托。 谢谢您的帮助。
编辑:更改了我的应用程序以使用新的iOS5 UIViewController包含。 好多了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.