UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer  、NSUserDefaults)

本文介绍iOS应用中使用UINavigationController进行界面导航的方法,包括创建导航控制器、页面跳转及返回等操作,并详细讲解了不同场景下如何通过属性、代理等方式在视图控制器间传递数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.UINavigationController
 
   
//  创建一个导航控制器
   
// 创建一个控制器作为根控制器 去管理
   
RootViewController *rootVC = [[RootViewController alloc]init];
   
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];
    [rootVC
release];
   
// 把导航控制器作为window的根控制器
   
self.window.rootViewController = navC;
    [navC release];
- (void)viewDidLoad {
    [
super viewDidLoad];
   
self.view.backgroundColor = [UIColor greenColor];
   
   
// 创建一个按钮 进入下一个页面
   
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.
frame = CGRectMake(100, 100, 100, 100);
    [
self.view addSubview:button];
    button.
backgroundColor = [UIColor redColor];
    [button
setTitle:@"2" forState:(UIControlStateNormal)];
    [button
release];
   
// 添加方法 进入下一个界面
    [button
addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchDragInside)];


// 实现Button的方法  进入下一个界面

- (
void)actionButton:(UIButton *)button
{
   
// 创建下一个界面的
   
SecondViewController *secondVC = [[SecondViewController alloc]init];
   
// ViewControllers 是导航控制器管理的那一组控制器 所有被管理的控制器们都存放在这个数组中
  
// NSLog(@"%@", self.navigationController.viewControllers);
   
// 跳转去下一个页面  push
    [
self.navigationController pushViewController:secondVC animated:YES];
    [secondVC
release];
  
// NSLog(@"%@", self.navigationController.viewControllers);
   
// 栈顶的控制器
   
// 正显示的控制器
   
NSLog(@"栈顶%@", self.navigationController.topViewController);
  
   
   
}

 // 返回上一个界面
    [
self.navigationController popViewControllerAnimated:YES];
   
   
}
- (
void)actionSecondButton:(UIButton *)button
{
   
   
   
// 返回根界面
    [
self.navigationController popToRootViewControllerAnimated:YES];
   
   
}
- (
void)actionThirdButton:(UIButton *)button
{
   
// 返回到指定界面
   
// 需要从被管理的数组中 把要返回的取出来
   
NSArray *viewControllers = self.navigationController.viewControllers;
   
SecondViewController *sec = viewControllers[1];
    [
self.navigationController popToViewController:sec animated:YES];
   
// 或者下面的
    //[self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];




    
状态栏高  20
     bar 
  44
    
     */

   
self.view.backgroundColor = [UIColor redColor];
   
self.navigationItem.title = @"首页";
   
// 创建一个UIView出来给titleView   有了View 想往上面加什么都随意
   
//UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 44)];
  
// view.backgroundColor = [UIColor whiteColor];
   
//self.navigationItem.titleView = view;
   
   
// 用系统样式初始化一个UIBarButtonItem
   
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(actionLeftBarButtonItem:)];
   
self.navigationItem.leftBarButtonItem = leftButton;
    [leftButton
release];
   
   
// 用标题的初始化方法 初始化右面的按钮
   
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@" " style:(UIBarButtonItemStylePlain) target:self action:@selector(actionRightBarButtonItem:)];
   
self.navigationItem.rightBarButtonItem = rightBarButton;
   
   
// 用图片初始化UIBarButtonItem
   
   
UIBarButtonItem *imageBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"left"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionImageBarButton:)];
   
   
self.navigationItem.rightBarButtonItem = imageBarButton;
    [imageBarButton
release];
   
   
// 设置一下Bar 的颜色及图片
   
 
//  [self.navigationController.navigationBar setBarTintColor:[UIColor grayColor]];
   
// 44 的把 导航条遮住了,漏出了状态栏
   
// 64 的把 导航栏和状态栏都充满
 
//  [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"32064"] forBarMetrics:(UIBarMetricsDefault)];
   
   
// 重要的属性
   
// 7.0 以后 导航条默认是半透明的  如果半透明 从屏幕左上角开始计算frame
   
// 如果设置成不透明的 从导航条的底部开始计算frame
   
// 透明度的设置
   
self.navigationController.navigationBar.translucent = NO;
   
//
   
   
   
   
// Do any additional setup after loading the view.
}
- (
void)actionLeftBarButtonItem:(UIBarButtonItem *)barButtonItem
{
   
NSLog(@"启动照相机");
}
- (
void)actionRightBarButtonItem:(UIBarButtonItem *)barButtonItem
{
   
NSLog(@"我是右面的ooo");
}
- (
void)actionImageBarButton:(UIBarButtonItem *)barButtonItem
{
    NSLog(@"图片的按钮 ");





二.界面传值
属性传值
     *  从前往后( ) 是用属性传值
     1.
点击跳转方法的时候传值
     2.
要传递的是什么? 传递的是数据不是控件
     */
} // 要拿到要传递的数据
   
UITextField *textField = (UITextField *)[self.view viewWithTag:888];
   
NSString *string = textField.text;
   
// 要有一个属性来接收这个数据
   
SecondViewController *second = [[SecondViewController alloc]init];
    second.string = string;




代理传值
 *  从后往前( )用代理传值
 1. 协议从哪儿写?   后面的界面写
@protocol SecondDelegate <NSObject>

- (
void)changeTextFieldString:(NSString *)string;
@end
@interface SecondViewController : UIViewController
@property (nonatomic,retain)NSString *string;
@end
 2. 协议中要有的方法 传值用  方法中的参数就是咱们要传得值
 3. 顶搞一个代理的属性出来 设置代理 来使用
@property (nonatomic,retain)id<SecondDelegate> delegate;
 4. 根遵守协议 然后 根设置代理 根实现方法
@interface RootViewController : UIViewController<SecondDelegate>
- (void)changeTextFieldString:(NSString *)string
{
   
SecondViewController *second = [[SecondViewController alloc]init];
   
// 设置代理
   
   
UITextField *textField = (UITextField *)[self.view viewWithTag:888];
    textField.
text = string;
}
- (void)actionButton:(UIButton *)button
{
   
   
// 要拿到要传递的数据
   
UITextField *textField = (UITextField *)[self.view viewWithTag:888];
   
NSString *string = textField.text;
   
// 要有一个属性来接收这个数据
   
SecondViewController *second = [[SecondViewController alloc]init];
    second.
string = string;
   
// 设置代理
    second.
delegate = self;
   
    [
self.navigationController pushViewController:second animated:YES];
}
 5. 回到后一个界面 顶让代理调用方法进行传值
- (void)actionButton:(UIButton *)button
{
   
UITextField *textField = (UITextField *)[self.view viewWithTag:999];
  
    [
_delegate changeTextFieldString:textField.text];
   
   
    [
self.navigationController popToRootViewControllerAnimated:YES];
}
 6. 根拿到传过来的参数进行赋值
 */

三. NSTIMER
//NSTimer
    // 创建一个定时器  定时循环执行某一个方法  (NSTimeInterval) 时间的间隔  如下 1.0 就是每1.0秒执行一次方法  userInfo  可以当成一个参数  也可以标识这个定时器是干什么的
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timer:) userInfo:@"haha" repeats:YES];
    
    // 激活定时器  相当于启动
    [timer fire];    
    // 停止
    [timer invalidate];


四.NSUserDefaults


// NSUserDefaults  最轻量级的持久化
   
   
// 一般只用这个来存储  例如: 账号/密码/设备ID... ...
   
// 单例, 目前就叫初始化一个对象就行
   
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   
// 保存
    [userDefaults
setObject:@"尚亚雷" forKey:@"shang"];
   
// 同步数据
    [userDefaults
synchronize];
   
   
// 打印沙盒路径
   
NSLog(@"%@", NSHomeDirectory());
   
/*
     /Users/lanou3g/Library/Developer/CoreSimulator/Devices/F4C0035C-BFC9-4B9A-95D1-59CE94B63E35/data/Containers/Data/Application/1F290708-5666-45D8-9222-AFAD1EC03E16
     */

   
   
/*
     //
取出沙盒里面保存的数据
     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
     NSString *string = [userDefaults objectForKey:@"shang"];
     NSLog(@"%@", string);
     */


// 取出沙盒里面保存的数据
   
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   
NSString *string = [userDefaults objectForKey:@"shang"];
    NSLog(@"%@", string);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值