2012-02-29 73 views
2

我希望能夠在每次用戶點擊按鈕時在單獨的UIViewController中創建新的UI元素(UIProgressView欄)。如何以編程方式創建UI元素

我怎麼會去這樣做呢?

+3

我不知道你的意思。基於viewController1中的操作,您希望在viewController2出現時顯示進度視圖?如果是這樣,你可以使用viewController2偵聽的通知。收到通知時創建進度視圖。 – 2012-02-29 02:29:51

+0

完全是這樣的...使用通知與使用appDelegate有何不同? – iggy2012 2012-02-29 04:40:59

回答

4

以編程方式創建一個UIProgressView,它是簡單地使用alloc和初始化,設置幀的事情,添加一個子視圖,就像這樣:

//.h 

#import <UIKit/UIKit.h> 

@interface ExampleViewController : UIViewController { 

    //create the ivar 
    UIProgressView *_progressView; 

} 
/*I like to back up my iVars with properties. If you aren't using ARC, use retain instead of strong*/ 
@property (nonatomic, strong) UIProgressView *progressView; 

@end 

//.m 

@implementation 
@synthesize progressView = _progressView; 

-(void)viewDidLoad { 
    /* it isn't necessary to create the progressView here, after all you could call this code from any method you wanted to and it would still work*/ 
    //allocate and initialize the progressView with the bar style 
    self.progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar]; 
    //add the progressView to our main view. 
    [self.view addSubview: self.progressView]; 
    //if you ever want to remove it, call [self.progressView removeFromSuperView]; 

} 
+0

非常感謝。 – iggy2012 2012-02-29 03:16:12

+0

沒問題。如果你不知道如何編寫用戶界面,我建議你儘快骨化! – CodaFi 2012-02-29 03:27:31