0
编程方式添加一个按钮与外部类:添加按钮编程
文件头:
-(UIView*) makeButtonNotification;
文件的方法:
-(UIView*) makeButtonNotification{
UIImage * buttonImage = [UIImage imageNamed:@"chat-notify.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(goChat)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(280.0, 25.0, 30.0, 30.0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
return button;
}
此按钮呼叫goChat方法
- (void)goChat{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
chat = (chatViewController *)[storyboard instantiateViewControllerWithIdentifier:@"chatViewController"];
chat.view.layer.zPosition = 10;
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:chat];
}
我添加的看法与此代码:
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:chat];
因为otherways我有层次
谁的观点是不是在窗口层次的问题!
我所说的方法是这样的:
notificationViewController* notification = [[notificationViewController alloc]init];
[self.view addSubview:[notification makeButtonNotification]];
现在的观点出现,这是没问题的。
但是当出现我有一个按钮使故事板打开左侧面板菜单,并没有工作。
还有一些其他解决方案?
谢谢。