UI控件6 UIViewController(1)详细解析+纯代码

 

1.UIViewController的概念
2.UIViewController的属性
3.UIViewController的基本用法
4.UIKit框架代码介绍

把info.plist文件中的 Main storyboard file base name文件删去
在AppDelegate.h文件中 重写

AppDelegate.m文件的使用:

#import "AppDelegate.h"
//导入视图控制器
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //先创建一个UIWindow对象
    //属于Appdelegate的属性
    //UIScreen:表示屏幕硬件类
    //mainScreen:获得主屏幕的信息
    //.bounds 获得当前手机屏幕的大小尺寸
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    //创建视图控制器对象
    ViewController * VCRoot= [[ViewController alloc]init];
    
    //对窗口的根视图控制器进行赋值操作
    //整个UIKit框架中只有一个根视图控制器,属于window属性
    //视图控制器用来管理界面和处理逻辑对象
    //程序启动前必须对根视图控制器进行赋值
    self.window.rootViewController = VCRoot;
    
    //将window作为主视图并且显示出来
    [self.window makeKeyAndVisible];
    
    
    
    // Override point for customization after application launch.
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end

ViewController.h文件的声明:

#import <UIKit/UIKit.h>

//所有的视图控制器都需要自定义来完成
//继承于官方的UIViewController
@interface ViewController : UIViewController


@end

ViewController.h文件的使用

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
//当视图控制器第一次被加载显示视图时,调用此函数
//布局初始化视图使用,初始化资源使用
- (void)viewDidLoad {
    //调用父亲类的加载视图函数
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView * view = [[UIView alloc]init];
    
    view.frame = CGRectMake(100, 100, 100, 200);
    
    //将视图添加到当前控制视图上
    [self.view addSubview:view];
    
    view.backgroundColor = [UIColor blueColor];
    
    self.view.backgroundColor = [UIColor orangeColor];
}

//当系统内存过低时,会发起警告信息,调用此函数
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    NSLog(@"内存过低!");
}


@end

main.m文件的实现:

//整个程序APP的入口函数
int main(int argc, char * argv[]) {
    @autoreleasepool {
        //UIKit 框架结构的启动函数
        //参数1:argc: 启动时带有参数的个数
        //参数2:argv:  参数列表
        //参数3:要求传入一个主框架类类名,如果参数为nil 系统会自动用默认的框架类作为主框架类
        //参数4:主框架的代理类对象
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Objective-C 代码实现签到界面的步骤如下: 1. 创建一个 UIViewController。 ``` #import <UIKit/UIKit.h> @interface SignInViewController : UIViewController @end ``` 2. 在 UIViewController 中添加需要的 UI 元素。 ``` @interface SignInViewController () @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *descriptionLabel; @property (nonatomic, strong) UILabel *dateLabel; @property (nonatomic, strong) UIButton *signInButton; @end @implementation SignInViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)]; self.titleLabel.textAlignment = NSTextAlignmentCenter; self.titleLabel.text = @"签到"; [self.view addSubview:self.titleLabel]; self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, 30)]; self.descriptionLabel.textAlignment = NSTextAlignmentCenter; self.descriptionLabel.text = @"签到可以获得积分哦"; [self.view addSubview:self.descriptionLabel]; self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 30)]; self.dateLabel.textAlignment = NSTextAlignmentCenter; self.dateLabel.text = [self getCurrentDate]; [self.view addSubview:self.dateLabel]; self.signInButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100) / 2, 250, 100, 50)]; [self.signInButton setTitle:@"签到" forState:UIControlStateNormal]; [self.signInButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [self.signInButton addTarget:self action:@selector(signInButtonAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.signInButton]; } - (NSString *)getCurrentDate { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSString *currentDate = [formatter stringFromDate:[NSDate date]]; return currentDate; } - (void)signInButtonAction { // 处理签到逻辑 } @end ``` 3. 在需要的地方创建 SignInViewController 并展示。 ``` SignInViewController *signInViewController = [[SignInViewController alloc] init]; [self.navigationController pushViewController:signInViewController animated:YES]; ``` 以上就是 Objective-C 代码实现签到界面的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值