2014-01-24 92 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];