Hello World for IOS

2017-12-12  本文已影响54人  专家搬运工

指定app 初始页面(不使用storyboard)

1、创建工程:xcode>File >New >Project > IOS>Single View App>.......
2、删除main.storyboard。同时将设置页面的“General->Development Info->Main Interface”栏中内容清空。
3、修改AppDelegate.m中的didFinishLaunchingWithOptions 指定首页为MainViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //设置窗口
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //初始页面
    UIViewController *mainVC = [[MainViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
    //是否隐藏导航栏
    nav.navigationBarHidden = YES;
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}



4、MainViewController示例

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //将背景设置为白色
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn;
    //y起始坐标为20,因为状态栏高度是20
    btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 100, 40)];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor redColor]];
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

-(void)click:(id)sender{
    NSLog(@"btn被点击了");
//   跳转到下一个页面
//    NewsListTableViewController *nextPage= [[NewsListTableViewController alloc]init];
//    nextPage.hidesBottomBarWhenPushed=YES;
//    self.navigationController.navigationBarHidden = NO;
//    [self.navigationController pushViewController:nextPage animated:YES];
}
4、效果 屏幕快照 2017-12-12 下午8.44.42.png
上一篇下一篇

猜你喜欢

热点阅读