IOS-UI UI初步代码布局添加事件

ISO开发界面,UI是必须学习的一部分,其实很早以前想学来了,一直没有沉下心来学习。看到IOS的代码风格和布局就别扭的不行,跟java代码和android布局比较显得不是那么方便,因此一直到如今。先看一段代码java

头文件:
//
//  RootViewController.h
//  UIPickerViewDemo
//
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end



实现文件
//
//  RootViewController.m
//  UIPickerViewDemo
//

#import "RootViewController.h"

@interface RootViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;

- (IBAction)onClick:(id)sender;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    CGRect screen = [[UIScreen mainScreen] bounds];
    CGFloat screenHeight = screen.size.height;
    CGFloat screenWidth = screen.size.width;
    NSLog(@"(%0.00f,%0.00f)",screenWidth,screenHeight);
    
    CGRect buttonFrame = CGRectMake(0, screenHeight*3/5, screenWidth, screenHeight/10);
    UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
    [button setTitleColor:UIColor.greenColor forState:UIControlStateNormal];
    [button setTitle:@"底部按钮" forState:UIControlStateNormal];
    [button addTarget:self 
               action:@selector(onButtonClick:)
               forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}


- (void) onButtonClick:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请确认" message:@"这是弹窗控件的消息message!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"CANCLE" 
        style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"action cancle!");
    }];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
@end


自定义的UIViewController 须要跟AppDelegate先创建链接

//
//  AppDelegate.m
//  UIPickerViewDemo
//
#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (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]initWithNibName:@"RootViewController" bundle:nil];
    
    [self.window makeKeyAndVisible];
    return YES;
}

//省略 只看重点...

@end

其实上面只是先将main.storyboard和xcode自动创建的ViewController.h,ViewController.m文件删除了,而后本身建立了一个RootViewController文件,而后使用它来进行布局,能够一块儿使用xib来布局或者代码布局,可是只是这样运行起来仍是看不到界面,由于工程仍是会默认加载main.stroyboard,因此就须要修改工程属性,按照下面步骤:android

1)delete 掉原来的三个文件ViewController.h、ViewController.m、main.storyboardxcode

2)General --> TARGETS --> UIPickerViewDemo -->Deployment Info --> Main Interface 删除Main Interface中的Main,空着,以下图app

3)添加视图控制器ide

Xcode -->  File --> New --> file布局

选择Cocoa Touch Clas --> Next弹出下面图,写入类名RootViewController,勾选Also create XIB file -->Next学习

 

而后就是创建链接,须要修改AppDelegate代码atom

自定义的UIViewController 须要跟AppDelegate先创建链接,见上面源代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.rootViewController =  [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
    [self.window makeKeyAndVisible];
    return YES;
}spa

到此运行command + R 运行,点击“底部按钮”看效果,以下如code

相关文章
相关标签/搜索