如何在按钮单击的同一窗口中加载新视图?

时间:2012-09-19 06:14:21

标签: objective-c cocoa-touch

我有一个带按钮的View-Controller。我希望在按下按钮时将新视图加载到 View-Controller 上。它不会取代现有的视图,我希望它小于屏幕,并在我点击小视图时隐藏。

如何在代码中实现?

2 个答案:

答案 0 :(得分:3)

- (IBAction)button:(id)sender 
{
UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(0,0,200,200)];
[self.view addSubview:view2];
UITapGestureRecognizer *Tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Tapview)] autorelease];
[view2 addGestureRecognizer:Tap];
}

-(void)Tapview
{
[view2 removeFromSuperview];
}

答案 1 :(得分:2)

将自拍添加到自我视图中,如下所示:

 UITapGestureRecognizer *oneFinger = 
 [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerAction:)] autorelease];

// Set required taps and number of touches
[oneFinger setNumberOfTapsRequired:1];
[oneFinger setNumberOfTouchesRequired:1];

// Add the gesture to the view
[[self view] addGestureRecognizer:oneFinger];

在.h文件中添加一个BOOL标志;在ViewDidLoad方法中添加:

 flag = FALSE;

现在我假设你有UIView * smallView,可以像这样在屏幕上添加:

[self.view addSubView:smallView];
flag = TRUE;
smallView.center = self.view.centre;

现在点击了self.view点击手势动作

- (void)oneFingerAction:(UITapGestureRecognizer*)sender
{
   if(sender.view == self.view)
   { 
     if(flag){
       if(smallView)
       {
         [smallView removeFromSuperView];
       }
     }
   }
}