2014-01-24 91 views
-1

我想在iOS中開始使用UI編程,我的第一個移動應用程序主要使用故事板,所以我希望我的UI更加靈活。我一直在線上跟隨一對夫婦的教程,我仍然無法讓我的視圖和子視圖在運行Xcode模擬器時顯示出來。我用一個viewController創建了一個單一視圖應用程序。這是我的「ViewController.h」和「ViewController.m」文件中的代碼。我在這裏做錯了什麼人?我的模擬器沒有顯示任何內容,根據我一直遵循的教程應該是。以編程方式創建視圖iPhone編程

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property(strong, nonatomic) UILabel *label1; 
@property(strong, nonatomic) UIButton *someButton; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    // Create the label 
    self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)]; 
    self.label1.text = @"I am a label"; 

    // Create the button 
    self.someButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    self.someButton.frame = CGRectMake(10, 50, 100, 37); 
    [self.someButton setTitle:@"Example Button" forState:UIControlStateNormal]; 

    // Add them to the main view 
    [self.view addSubview:self.label1]; 
    [self.view addSubview:self.someButton]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

你有沒有指定在故事板'ViewController'類控制器? –

+0

你是什麼意思? – AyBayBay

+0

@Virussmca是的我做過 – AyBayBay

回答

0

嗯,你的代碼在我的Xcode工作正常,我創造了新的單一視圖,基於應用和刪除的故事板和複製粘貼代碼

I am able to see the label and Button

+0

我沒有從我的項目中刪除故事板,這可能是問題嗎? – AyBayBay

+0

其實我只是試圖刪除故事板,它仍然沒有區別 – AyBayBay

+0

你會介意在github要點上發佈項目代碼中的所有類嗎?如果你能告訴我你正在運行的是什麼模擬器(如iPhone Retina 3.5或4) – AyBayBay

0

當你progra以創建一個UIView,UIButton或UILable,沒有定義任何佈局約束。你必須手動添加它們。如果確實有一些佈局約束配置爲某個視圖,則可以像這樣刪除它們:

[view removeConstraints:view.constraints] 
0

確保您首先顯示ViewController。

1

創建一個沒有故事板一個新的項目,並把下面的代碼,它會運行

UILabel *testLabel; 
testLabel=[[UILabel alloc]init]; 
[email protected]"Hello World!"; 
testLabel.frame = CGRectMake(10, 50, 100, 37); 
[self.view addSubview:testLabel];