如何从Button Action引用外部UIView

时间:2014-07-17 17:47:42

标签: ios objective-c uiview

我是iOS开发的新手。我以编程方式创建了一个UIView和一个按钮:

- (void)viewDidLoad
{
[super viewDidLoad];

UIView* mainView = self.view;
mainView.backgroundColor = [UIColor blackColor];

UIView* rotateView = [[UIView alloc] initWithFrame:CGRectMake(30, 120, 260, 260)];
rotateView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Wheel"]];
rotateView.layer.cornerRadius =130;
rotateView.tag = 1;
[mainView addSubview:rotateView];



UIButton *btnAct = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnAct addTarget:self
           action:@selector(btnClicked:)
 forControlEvents:UIControlEventTouchUpInside];
[btnAct setTitle:@"GO" forState:UIControlStateNormal];
btnAct.frame = CGRectMake(160.0, 450.0, 80.0, 80.0);
[mainView addSubview:btnAct];


}
-(void)btnClicked:(id)sender{

CGRect bezViewFrame = CGRectMake(122, 120, 77, 94);
UIView* testView2 = [[BBbezierView alloc]initWithFrame:bezViewFrame];
testView2.backgroundColor   = [UIColor clearColor];

UIView* Views1 = [[UIView alloc] viewWithTag:1];
Views1.backgroundColor =[UIColor whiteColor];
[Views1 addSubview:testView2 ];


}

但它不起作用。我想创建我的BBbezierView'作为' rotateView'。

的子视图

1 个答案:

答案 0 :(得分:0)

那一行

UIView* Views1 = [[UIView alloc] viewWithTag:1];

首先创建一个新的UIView,然后尝试使用标记1在其上查找视图。当然,这不存在,因此返回nil。您必须在viewController的视图中搜索它。为了更好地读取代码,请使用小写字母命名变量名称:

UIView* rotateView = [self.view viewWithTag:1];