2014-02-21 44 views
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]]; 

现在的观点出现,这是没问题的。

但是当出现我有一个按钮使故事板打开左侧面板菜单,并没有工作。

还有一些其他解决方案?

谢谢。

回答

0

您的makeButtonNotification正在创建并返回一个UIButton,但您的返回类型设置为UIView?另外,你有一个按钮的类属性?由于您不是在该方法中创建本地实例?

你打电话给这个goChat的方法,我认为它不在appDelegate之外?

您将按钮帧的原点设置为280,您确定这是在self.view的范围内吗?你将它添加到什么视图?