我基本上只需要當用戶點擊我的自定義UIView的一個按鈕來更改選項卡更改標籤
這是我的UIView實現
@implementation CustomMenuView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 135.0, 40.0)];
searchButton.backgroundColor = [UIColor clearColor];
[searchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
searchButton.titleLabel.font = [UIFont systemFontOfSize:15];
[searchButton setTitle:@"Search" forState:UIControlStateNormal];
searchButton.tag = 0;
[searchButton addTarget:self action:@selector(menuItemTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:searchButton];
}
return self;
}
-(void)menuItemTapped:(UIButton*)sender{
self.tabBarController.selectedIndex = sender.tag;
}
而且我的ViewController類:
UIView *menuView;
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat height = [UIScreen mainScreen].bounds.size.height;
menuView = [[CustomMenuView alloc]initWithFrame:CGRectMake(0, 60, 135, height)];
[self.view addSubview:menuView];
}
由於UIView沒有對tabBarController的引用,因此崩潰。如何在我的自定義視圖的父級上調用方法或者解決此問題的最佳方法是什麼?
感謝我upvoted和敢打賭,這工作得很好,但我還是決定爲此使用本地通知。 –
當然,很高興你找到了解決方案。請記住,使用NSNotification這些東西往往會被認爲是反模式有些(見[這個偉大的職位(https://davidnix.io/post/stop-using-nsnotificationcenter/))。不過,我猜想真正重要的是它完成了工作:) –