页面之间的数据传递是最基本的,下面就ios页面之间的数据传递需要理解的知识点整理一下
1,了解委托代理
2,协议的定义和实现(java的接口,Android的页面跳转使用的是Intent 所以不需要使用接口)
3,定义空白的window等(本人觉得最难的是按照网上的方法创建工程时,有一些方法找不到,建议在这一块找一个做ios开的人员指导一下)
一:普通数据传递 (ViewController页面输入框的值传到ViewController2页面并显示在输入框中)
a,定义ViewController和ViewController2页面并添加数据框和按钮
b,添加按钮的点击事件
c,将数据通过属传递到ViewController2页面
代码实现如下:
// // ViewController.h // test1114 // // Created by wang on 15/11/14. // Copyright © 2015年 wang. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
ViewController.m
//
// ViewController.m
// test1114
//
// Created by wang on 15/11/14.
// Copyright © 2015年 wang. All rights reserved.
//
#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()
@property UITextField* tf1;
@property UITextField* tf2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
self.navigationItem.title=@"第一个界面";
//姓名输入框
_tf1= [[UITextField alloc]init];
_tf1.frame=CGRectMake(100, 100, 200, 30);
//设置边框
_tf1.borderStyle=UITextBorderStyleRoundedRect;
//设置输入框的背景颜色
_tf1.backgroundColor=[UIColor whiteColor];
// 设置提示语
_tf1.placeholder=@"请输入姓名";
[self.view addSubview:_tf1];
[_tf1 release];
//创建button
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//butto的大小
btn.frame = CGRectMake(20, 100, 100, 300);
//标题
[btn setTitle:@"next" forState:UIControlStateNormal];
//跳转
[btn addTarget:self action:@selector(nextA) forControlEvents:UIControlEventTouchUpInside];
//添加到view视图中
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//点击事件
- (void)nextA
{
ViewController2 *viewCtr = [[ViewController2 alloc] init];
viewCtr.userNameTitle=_tf1.text;//将输入框的值传递到第二个界面
[self.navigationController pushViewController:viewCtr animated:YES];
[viewCtr release];
}
@end
第二个页面
//
// ViewController2.h
// test1114
//
// Created by wang on 15/11/15.
// Copyright © 2015年 wang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
@property(nonatomic,copy) NSString* userNameTitle;
@end
.m文件
//
// ViewController2.m
// test1114
//
// Created by wang on 15/11/15.
// Copyright © 2015年 wang. All rights reserved.
//
#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController2 ()
@property UITextField* tf1;
@end
@implementation ViewController2
@synthesize userNameTitle=_userNameTitle;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title=@"第二个页面";
//姓名输入框
_tf1= [[UITextField alloc]init];
_tf1.frame=CGRectMake(100, 100, 200, 30);
//设置边框
_tf1.borderStyle=UITextBorderStyleRoundedRect;
//设置输入框的背景颜色
_tf1.backgroundColor=[UIColor whiteColor];
// 设置提示语
_tf1.placeholder=@"请输入姓名";
_tf1.text=_userNameTitle;
[self.view addSubview:_tf1];
[_tf1 release];
UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(10, 200, 300, 200);
[btn setTitle:@"跳转到第一个页面" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(ShowNext) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:btn];
// Do any additional setup after loading the view.
}
- (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.
}
*/
-(void)ShowNext{
//点击按钮返回到上一个界面,并将输入框的参数带到上一个界面
NSString* btn=_tf1.text;
NSLog(@"%@",_tf1.text);
//设置视图出栈
[self.navigationController popViewControllerAnimated:YES];
// ViewController3 * vc3 = [[ViewController3 alloc]init];
//
// [self.navigationController pushViewController:vc3 animated:YES];
//
// [vc3 release];
}
@end
2,第二页面输入框的值传递到第一个页面
//
// ViewController.h
// test1114
//
// Created by wang on 15/11/14.
// Copyright © 2015年 wang. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ViewController2.h"
/**
实现代理,接收ViewController2代理传过来的值
*/
@interface ViewController : UIViewController<IntentFrist>
@end
//
// ViewController.m
// test1114
//
// Created by wang on 15/11/14.
// Copyright © 2015年 wang. All rights reserved.
//
#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()
@property UITextField* tf1;
@property UITextField* tf2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
self.navigationItem.title=@"第一个界面";
//姓名输入框
_tf1= [[UITextField alloc]init];
_tf1.frame=CGRectMake(100, 100, 200, 30);
//设置边框
_tf1.borderStyle=UITextBorderStyleRoundedRect;
//设置输入框的背景颜色
_tf1.backgroundColor=[UIColor whiteColor];
// 设置提示语
_tf1.placeholder=@"请输入姓名";
[self.view addSubview:_tf1];
[_tf1 release];
//创建button
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//butto的大小
btn.frame = CGRectMake(20, 100, 100, 300);
//标题
[btn setTitle:@"next" forState:UIControlStateNormal];
//跳转
[btn addTarget:self action:@selector(nextA) forControlEvents:UIControlEventTouchUpInside];
//添加到view视图中
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//点击事件
- (void)nextA
{
ViewController2 *viewCtr = [[ViewController2 alloc] init];
viewCtr.userNameTitle=_tf1.text;//将输入框的值传递到第二个界面
viewCtr.delegate=self;
[self.navigationController pushViewController:viewCtr animated:YES];
[viewCtr release];
}
#pragma mark -实现协议,并接收传值
-(void)getUserNameTitle:(NSString *)userNameTitle{
NSLog(@"%@",userNameTitle);
_tf1.text=userNameTitle;
}
@end
第二个页面
//
// ViewController2.h
// test1114
//
// Created by wang on 15/11/15.
// Copyright © 2015年 wang. All rights reserved.
//
#import <UIKit/UIKit.h>
/**
使用协议代理传值
*/
@protocol IntentFrist <NSObject>
-(void)getUserNameTitle:(NSString*) userNameTitle;
@end
@interface ViewController2 : UIViewController
//类实现委托代理
@property(nonatomic,assign)id<IntentFrist> delegate;//实现代理
@property(nonatomic,copy) NSString* userNameTitle;
@end
//
// ViewController2.m
// test1114
//
// Created by wang on 15/11/15.
// Copyright © 2015年 wang. All rights reserved.
//
#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController2 ()
@property UITextField* tf1;
@end
@implementation ViewController2
@synthesize userNameTitle=_userNameTitle;
@synthesize delegate=_delegate;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title=@"第二个页面";
//姓名输入框
_tf1= [[UITextField alloc]init];
_tf1.frame=CGRectMake(100, 100, 200, 30);
//设置边框
_tf1.borderStyle=UITextBorderStyleRoundedRect;
//设置输入框的背景颜色
_tf1.backgroundColor=[UIColor whiteColor];
// 设置提示语
_tf1.placeholder=@"请输入姓名";
_tf1.text=_userNameTitle;
[self.view addSubview:_tf1];
[_tf1 release];
UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(10, 200, 300, 200);
[btn setTitle:@"跳转到最后一个页面" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(ShowNext) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:btn];
// Do any additional setup after loading the view.
}
- (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.
}
*/
-(void)ShowNext{
//点击按钮返回到上一个界面,并将输入框的参数带到上一个界面
NSString* btn=_tf1.text;
NSLog(@"%@",_tf1.text);
// if ([_delegate respondsToSelector:@selector(nextA:)]) {
[_delegate getUserNameTitle:_tf1.text];
// }
//设置视图出栈
[self.navigationController popViewControllerAnimated:YES];
// ViewController3 * vc3 = [[ViewController3 alloc]init];
//
// [self.navigationController pushViewController:vc3 animated:YES];
//
// [vc3 release];
}
@end
解释:第二个页面的数据传递到第一个页面,
1),先讲第二个页面的值放到协议中 [_delegate getUserNameTitle:_tf1.text];
2),第一个页面实现协议并接收数据
#pragma mark -实现协议,并接收传值
-(void)getUserNameTitle:(NSString *)userNameTitle{
NSLog(@"%@",userNameTitle);
_tf1.text=userNameTitle;
}
android页面之间的数据传递;
布局就一个输入框和按钮
第一个页面
package com2.example.wang.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText input_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//初始化输入框
input_text= (EditText)findViewById(R.id.index_et);
}
/**
* 按钮点击事件
* @param view
*/
public void show(View view){
switch (view.getId()){
case R.id.index_btn:
//获得输入框的值,将值传到下一页
startActivity(new Intent(MainActivity.this,Main2Activity.class).putExtra("userNameTitle",input_text.getText().toString().trim()));
break;
case R.id.index_btn2:
//获得输入框的值,将值传到下一页
Intent intent =new Intent();
intent.setClass(MainActivity.this,Main2Activity.class);
intent.putExtra("userNameTitle",input_text.getText().toString().trim());
startActivityForResult(intent, 2);
break;
default:
break;
}
}
/**
* 获得第二个界面传递的值
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK&&requestCode==2){
input_text.setText(data.getStringExtra("userNameTitle"));
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
*
* @param menu
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
第二个页面
package com2.example.wang.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
public class Main2Activity extends AppCompatActivity {
private EditText inputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//初始化组件
inputText= (EditText)findViewById(R.id.et);
//将传递的值设置到输入框中
inputText.setText(getIntent().getStringExtra("userNameTitle"));
}
/**
* 按钮点击事件
* @param view
*/
public void show(View view){
switch (view.getId()){
case R.id.btn:
//获得输入框的值,将值传到下一页
Intent intent =new Intent();
// intent.setClass(Main2Activity.this, MainActivity.class);
intent.putExtra("userNameTitle", inputText.getText().toString().trim());
setResult(RESULT_OK,intent);
finish();
break;
default:
break;
}
}
}
android的跳转是通过Intent实现的,数据也可以直接放在Intent对象中传递
反向传值:
1,startActivityForResult
2,实现onActivityResult
3,第二个页面使用setResult带返回值跳转
4,综上 还是Android实现简单