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

相關文章
相關標籤/搜索