2010-04-01 72 views
1

我想通過編程方式來配置視圖。我有一個UIViewController,並希望添加一個UIButton到它的視圖。訪問一個viewControllers視圖

嗯,我創建的按鈕:

UIButton *newViewButton = [[UIButton alloc] initWithFrame:CGRectMake(baseX + viewPlusX, baseY + viewPlusY, viewWidth, viewHeight)]; 
[newViewButton setTitle:@"View" forState:UIControlStateNormal]; 
[newViewButton setTag:(int)key]; 
[newViewButton addTarget:myViewController action:@selector(viewButton:) forControlEvents:UIControlEventTouchUpInside]; 

,但試圖將它添加到視圖

[myViewController.view addSubview:newViewButton]; 

我得到的錯誤時

error: expected ':' before":" token 

任何線索,我是什麼做錯了

個問候

+0

你真的應該開始接受答案,幫助你。您通過點擊有用答案左上角的複選標記來接受答案。 – Till 2010-04-20 15:27:02

回答

2

@selector(viewButton:sender)應該讀@selector(viewButton:sender:)

3

使用

[myViewController.view addSubview:newViewButton]; 
+0

嗨,奧萊,其實我打錯了我的求救聲音(只是更新了它)。這是我如何得到它,並在哪裏我得到上述錯誤... – iFloh 2010-04-01 10:25:55

+0

它似乎是「myViewController.view」引用導致錯誤。 NSLog顯示錯誤「訪問未知」視圖「類方法」。 不明白這一點,因爲myViewController的類型是UIViewController。 – iFloh 2010-04-01 11:41:53

+0

好的,發現問題......笨...通過了課程,而不是對象.... – iFloh 2010-04-01 11:51:15