[英]Adding Custom UIButton To subView
我有一個子視圖,它是通過在主視圖中觸摸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添加一個操作來關閉Subview?
首先獲取subView的IBOutlet並將其添加到主視圖中。
IBOutlet UIView *subView;
然后,像這樣將UIButton添加到SubView中:
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 {
}
在上述方法中,從主視圖中刪除SubView。
將目標添加到自定義按鈕
{
.....
[button addTarget:self action:@selector(your dismiss:) forControlEvents:UIControlEventTouchUpInside];
.......
}
-(IBAction)your dismiss:(id)sender
{
// write the dismissal code here
}
如果只想為UIButton設置圖像而不是標題,則需要使用- (void)setImage:(UIImage *)image forState:(UIControlState)state
方法。
您應該檢查UIButton參考以獲取更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.