IOS学习之UiViewController带值跳转以及协议的实现(四)

2024-06-04 11:58

本文主要是介绍IOS学习之UiViewController带值跳转以及协议的实现(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们都知道Android里面的每个页面都是一个Activity,而Intent又是连接Activity的纽带,在IOS里面,每个页面都是一个UiViewController,而IOS中的跳转直装直接通过navigationController pushViewController:就可以了,还可以直接给跳转的页面赋值,而android里面是通过Intent把值传到第二页面,在IOS直接可以用UiviewController点出来,从后往前传值而是通过协议来实现,也就是android里面的所谓的接口。下面看看效果图:


我们新建了两个UiViewController,第一个传第二个直接赋值就可以了,第二个传第一个我们使的是协议。代码如下:

第一个FirstController:

//
//  FirstController.m
//  MyController
//
//  Created by xiaoyuan on 15/4/25.
//  Copyright (c) 2015年 xiaoyuan. All rights reserved.
//#import "FirstController.h"
#import "SecondController.h"@interface FirstController ()<changeVCDelegate>@end@implementation FirstController- (void)viewDidLoad {[super viewDidLoad];self.navigationController.navigationBar.barTintColor =[UIColor redColor];[self.navigationController.navigationBar setTranslucent:NO];UIButton*btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frame = CGRectMake(0, 100, 50, 50);btn.backgroundColor =[UIColor grayColor];CGPoint center = btn.center;center.x = self.view.center.x;btn.center = center;[btn setTitle:@"点我" forState:UIControlStateNormal];[btn addTarget:self action:@selector(goon) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btn];// Do any additional setup after loading the view.
}-(void) goon{SecondController*two =[[SecondController alloc]init];two.a = 1;//直接赋值two.delegate = self;[self.navigationController pushViewController:two animated:YES];//跳转
}
//协议方法第二个页面返回通过协议执行此方法
-(void)passValue:(NSString *)str sencode:(NSString *)secondStr
{UIAlertView *alreat  = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@",str] message:[NSString stringWithFormat:@"%@",secondStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];[alreat show];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end
第二个SecondController:这个我把.h跟.m文件都贴出来:

//
//  SecondController.h
//  MyController
//
//  Created by xiaoyuan on 15/4/25.
//  Copyright (c) 2015年 xiaoyuan. All rights reserved.
//#import <UIKit/UIKit.h>
//协议
@protocol changeVCDelegate <NSObject>-(void)passValue:(NSString *) str sencode:(NSString *)secondStr;@end@interface SecondController : UIViewController
@property (nonatomic ,assign)int a;
//声明代理
@property (nonatomic ,weak) id<changeVCDelegate> delegate;
@end
//
//  SecondController.m
//  MyController
//
//  Created by xiaoyuan on 15/4/25.
//  Copyright (c) 2015年 xiaoyuan. All rights reserved.
//#import "SecondController.h"@interface SecondController ()@end@implementation SecondController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];// Do any additional setup after loading the view.UILabel*text =[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 400, 100)];text.backgroundColor =[UIColor grayColor];[text setText:[NSString stringWithFormat:@"我是一个页面传过来的%d",self.a]];text.textAlignment =NSTextAlignmentCenter;[self.view addSubview:text];//返回键UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];self.navigationItem.leftBarButtonItem = rightButton;}//返回 通过协议把
-(void)backAction
{[self.delegate passValue:@"我是第二个页面传的值" sencode:@"我也是第二页面传回来的"];[self.navigationController popViewControllerAnimated:YES];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end

这样就实现了!大神勿喷。

源码下载










这篇关于IOS学习之UiViewController带值跳转以及协议的实现(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1030022

相关文章

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码