美文网首页
iOS学习-2

iOS学习-2

作者: Ello_Orld | 来源:发表于2019-11-12 22:28 被阅读0次
    1. 纯代码构建hello world

    新建项目,删除scene相关,删除ViewController,删掉Main Interface,添加RootViewController。
    修改AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = [[RootViewController alloc]init];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    RootViewController.m创建UILable并添加

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        CGRect screen = [[UIScreen mainScreen]bounds];
        CGFloat labelWidth = 90;
        CGFloat labelHeight = 20;
        CGRect frame = CGRectMake((screen.size.width - labelWidth) / 2, 150, labelWidth, labelHeight);
        UILabel *label = [[UILabel alloc]initWithFrame:frame];
        label.text = @"Hello World";
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:label];
    }
    

    运行查看结果:


    image.png
    2. UIView相关

    创建一个界面,包含一个文本和一个按钮,点击按钮改变文本内容。
    xlb实现:
    创建项目:


    image.png

    (1) Deployment Info target 删除iPad
    (2) Deployment Info Main Interface 清空
    (3) 删除SceneDelegate.h和SceneDelegate.m
    (4) 删除AppDelegate.m中的Scene相关代码。只保留didFinishLaunchingWithOptions方法。
    (5) Info.plist 删除Application Scene Manifest 项
    (6) AppDelegate.h添加

    @property(strong, nonatomic) UIWindow *window;
    

    (7) 创建界面,这里我们直接使用自动生成的ViewController,只创建一个xlb文件。
    右键-new File,选择View,创建名为ViewController.xib,点击create完成创建。
    打开xlb文件,点击左侧的File's Owner,在右侧的File's Owner Class中填入ViewController


    image.png

    然后点击左侧File's Owner图标,按住Ctrl键拖动到界面上


    image.png
    在弹出的Outlet选择view,完成。
    (8)修改AppDelegate.m文件
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    至此基础配置就完成来了,可以在xlb文件中写界面了。
    拖动一个UILabel和一个UIButton到界面中,如图


    image.png

    (9) 按住ctrl拖动view到控制器,如图


    image.png
    设置view的name
    同样拖动button到控制器,创建button的点击事件。
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UILabel *lebel;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    - (IBAction)buttonClick:(id)sender {
        self.lebel.text = @"Hello World";
    }
    
    
    @end
    

    运行可以实现效果。

    (10)使用纯代码方式构建:
    ViewController.m 代码

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property(strong, nonatomic) UILabel *label;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        CGRect screen = [[UIScreen mainScreen]bounds];
        CGFloat labelWidth = 90;
        self.label = [[UILabel alloc]initWithFrame:CGRectMake((screen.size.width - labelWidth) / 2, 80, 90, 20)];
        self.label.text = @"Original";
        self.label.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:self.label];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake((screen.size.width - 90) / 2, 200, 90, 20);
        [button setTitle:@"button" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
    -(void) buttonClick {
        self.label.text = @"Hello World";
    }
    
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS学习-2

          本文链接:https://www.haomeiwen.com/subject/ziccictx.html