2012-03-20 140 views
0

我有子视图,它通过触摸主视图中的UIbutton触发。 一旦子视图弹出,它就会显示带有我提供的信息的标签。这一切都有效。我只需要一个UIbutton来显示,这样我就可以设置它来关闭子视图并返回到主视图。我搜遍了,并没有发现任何有用的东西。添加自定义UIButton到子视图

这里是我在我的.m文件的代码如下所示:

////Button that Displays Subview with Its Label and Button 
- (IBAction)showInfo:(id)sender 
/////UI Subview 

{UIView *mySubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 480)]; 

[self.view addSubview:mySubview]; 
mySubview.backgroundColor = [UIColor blackColor]; 
mySubview.alpha = .7f; 

//////Label in SubView 

{UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 50)]; 
label.text = @"Here is info"; 
label.textColor = [UIColor whiteColor]; 
label.backgroundColor= [UIColor clearColor]; 

[self.view addSubview:label]; 


////Button in Subview 

//create the button 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:[UIImage imageNamed:@"button-info-x.png"]  forState:UIControlStateNormal]; 

//set the position of the button 
button.frame = CGRectMake(120, 252, 68, 68); 


//add the button to the view 
[self.view addSubview:button]; 

现在我只需要一个操作添加到UIButton的驳回子视图?

回答

5

首先将子视图的IBOutlet添加到主视图中。

IBOutlet UIView *subView; 

然后,添加的UIButton像这样子视图:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

//set the position of the button 
button.frame = CGRectMake(0, 0, 100, 30); 

//set the button's title 
[button setTitle:@"Click Me!" forState:UIControlStateNormal]; 

[btnMenu addTarget:self action:@selector(your MethodName:) forControlEvents:UIControlEventTouchUpInside]; 

//add the button to the view 
[subView addSubview:button]; 

然后,

-(IBAction)your MethodName:(id)sender { 


} 

在上述方法中,从主视图中删除子视图。

+0

我编辑了我的代码,并将其放在我的文章中。请看看,然后告诉我如何为按钮做出动作。我有点迷路 – Gabriel 2012-03-20 06:47:34

0

如果您只是想为UIButton而不是标题设置图片,那么您正在寻找- (void)setImage:(UIImage *)image forState:(UIControlState)state方法。

您应该检查UIButton reference了解更多信息。

+0

感谢我创建了UIButton的,并有图像了。现在我只需要UIbutton就可以关闭子视图。有什么帮助吗? – Gabriel 2012-03-20 05:50:49

+0

//创建按钮 UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@“button-info-x.png”] forState:UIControlStateNormal]; //设置按钮的位置 button.frame = CGRectMake(120,252,68,68); //将按钮添加到视图 [self.view addSubview:button]; – Gabriel 2012-03-20 05:51:41

+0

使用 - (void)addTarget:(id)目标动作:(SEL)动作forControlEvents:(UIControlEvents)controlEvents指定您需要单击按钮时触发哪个方法。现在,在这种方法中,我想你可以关闭子视图。 – Madhu 2012-03-20 05:55:48

0

添加目标的自定义按钮

{ 

..... 

    [button addTarget:self action:@selector(your dismiss:) forControlEvents:UIControlEventTouchUpInside]; 

....... 

} 
-(IBAction)your dismiss:(id)sender 
{ 

    // write the dismissal code here 

} 
相关问题