2011-04-21 17 views

回答

1

與嘗試以下

UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0); 
[playButton setTitle:@"Play" forState:UIControlStateNormal]; 
playButton.backgroundColor = [UIColor clearColor]; 
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ]; 
UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"]; 
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal]; 
UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"]; 
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted]; 
[playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:playButton]; 
+0

[self.view addSubview:playButton];這裏它顯示錯誤像請求成員視圖中的東西不在結構或工會 – Ammu 2011-04-26 07:14:57

+0

@Ammu:在哪個類(UIView或UIViewController)你正在創建UIButton .. – Jhaliya 2011-04-26 07:34:59

+0

@Ammu:支架在第一行不匹配......並且應該是UIButton * playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; – Jhaliya 2011-04-26 07:43:20

1

喜歡這個 -

自定義按鈕 -

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)]; 

系統BUTTOM -

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
+0

的UIButton * BT1 = [[UIButton的頁頭] initWithFrame:方法CGRectMake(10,20,0,0)]; \t [bt1 setTitle:@「1」forState:UIControlStateNormal]; [self.window makeKeyAndVisible]; [bt1 release]; – Ammu 2011-04-21 10:50:06

+0

通過唱這個它不顯示任何按鈕 – Ammu 2011-04-21 10:50:30

+0

你還需要添加它在你的視圖[self.windows addSubview:button]; – Saurabh 2011-04-21 10:52:01

2

你會需要聲明一個UIButton這樣的:

UIButton *newButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)]; 
[newButton setTitle:@"Some Button" forState:UIControlStateNormal]; 

你會然後添加屁股到一個視圖 -

[self.view addSubview: newButton]; // (could be another view other then self.view) 

最後,如果要在按該按鈕時,會發生一些動作,你可以這樣:

[newButton addTarget:self action:@selector(doSomething) 
    forControlEvents:UIControlEventTouchUpInside]; 

當然聲明功能:

- (void) doSomething 
{ 
     NSLog(@"Button pressed"); 
} 

當然不要忘記釋放按鈕。

+0

你的按鈕(newButton)泄漏 - 添加[newButton release];添加動作後; – Till 2011-04-21 12:16:31

+0

是的,我知道,這就是爲什麼我寫了「當然不要忘記釋放按鈕。」在結尾:) – BenB 2011-04-21 13:04:25

相關問題