1. Android--Activity的传值和回传值
- 通过startActivity来进行Activity的传值
页面一
//通过setClass方法来指定我们要跳转的Activity
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
//通过setAction方法来我们要完成的一个action操作
Intent intent = new Intent();
intent.setAction("com.xiaoluo.android_intent.second");
<activity
android:name="com.xiaoluo.android_intent.SecondActivity"
android:label="SecondActivity">
<intent-filter>
<action android:name="com.xiaoluo.android_intent.second"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
页面二
// 得到跳转到该Activity的Intent对象
Intent intent = getIntent();
int age = intent.getIntExtra("com.xiaoluo.android_intent.age", 10);
String name = intent.getStringExtra("com.xiaoluo.android_intent.name");
Bundle bundle = intent.getBundleExtra("bundle");
String world = bundle.getString("hello");
Log.i(TAG, age + ", " + name + ", " + world);
textView.setText(name + " : " + age + ", " + world);
System.out.println(intent);
- 通过startActivityForResult方法类得到Activity的回传值
Activity一
Intent intent = new Intent();
intent.putExtra("message", editText1.getText().toString().trim() + " + " + editText2.getText().toString().trim() + " = ?");
intent.setClass(MainActivity.this, SecondActivity.class);
/*
* 如果希望启动另一个Activity,并且希望有返回值,则需要使用startActivityForResult这个方法,
* 第一个参数是Intent对象,第二个参数是一个requestCode值,如果有多个按钮都要启动Activity,则requestCode标志着每个按钮所启动的Activity
*/
startActivityForResult(intent, 1000);
/**
* 所有的Activity对象的返回值都是由这个方法来接收
* requestCode: 表示的是启动一个Activity时传过去的requestCode值
* resultCode:表示的是启动后的Activity回传值时的resultCode值
* data:表示的是启动后的Activity回传过来的Intent对象
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000 && resultCode == 1001)
{
String result_value = data.getStringExtra("result");
editText3.setText(result_value);
}
}
Activity二
String result = editText.getText().toString().trim();
Intent intent = new Intent();
intent.putExtra("result", result);
/*
* 调用setResult方法表示我将Intent对象返回给之前的那个Activity,这样就可以在onActivityResult方法中得到Intent对象,
*/
setResult(1001, intent);
// 结束当前这个Activity对象的生命
finish();
/**
* 在调用了startActivity方法之后立即调用overridePendingTransition方法,选择页面跳转的动画模式
*/
overridePendingTransition(0,0);
- 传输对象
定义一个类
package com.example.jtr.helloworldapplication;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by jtr on 2017/12/5.
* Parcelable序列化接口
*/
public class User implements Parcelable {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
protected User(Parcel in) {
name = in.readString();
age = in.readInt();
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
if (getAge() != user.getAge()) return false;
return getName().equals(user.getName());
}
@Override
public int hashCode() {
int result = getName().hashCode();
result = 31 * result + getAge();
return result;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
}
在Activity中调用
//Activity一:
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
User user = new User("xiaoming",22);
intent.putExtra("user",user);
startActivity(intent);
startActivityForResult(intent,1000);
//Activity二:
User user =getIntent().getParcelableExtra("user");
2. IOS页面跳转方式
2.1 模态跳转(Model)
- 模态:一个普通的视图控制器一般只有模态跳转的功能,这个方法是所有视图控制器对象都可以用的。
(void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
在官方文档中,建议这两者之间通过delegate实现交互。例如使用UIImagePickerController从系统相册选取照片或者拍照,imagePickerController和弹出它的VC之间就通过UIImagePickerControllerDelegate实现交互的。
控制器的中的只读属性:presentedViewController和presentingViewController,他们分别就是被present的控制器和正在presenting的控制器。
Modal的效果:默认是新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止。但可以通过自定义转场来改变展现view的动画,大小,位置,是否移除跳转之前的view.这个效果可以用来模拟ipad特有的Popover弹出框。
需要注意的是,默认他的实现过程是移除跳转之前的控制器的view,并将新的控制器的view展示,但跳转之前的控制器并没有被释放,而是被强引用这的。区别于导航控制器的push。
通过 dismissViewControllerAnimated 来返回前一个界面的。
ModelJumpViewController *mvc = [ModelJumpViewController new];
[self presentViewController:mvc animated:YES completion:^{
NSLog(@"Model跳转后的回调");
}];
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"test,模态跳转的返回");
}];
2.2通过Segue跳转
//页面一的.m文件
//执行跳转
[self performSegueWithIdentifier:@"segueJump" sender:nil];
//监听segue方式的页面跳转传值
/*通过sender携带参数*/
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//获取目标页面对象
SegueJumpViewController *sjvc = (SegueJumpViewController *)segue.destinationViewController;
//通过给目标页面的成员变量赋值传输数据
sjvc.json = @"rootviewMsg";
//可通过回调方式接受目标页面的返回值
sjvc.cb = ^NSString *(NSString *msg) {
NSLog(@"%@",msg);
return @"来自页面一的传值";
};
}
//页面二的.h文件
#import "ViewController.h"
typedef NSString * (^callback)(NSString *msg);
@interface SegueJumpViewController : ViewController
@property(nonatomic,copy)NSString *json;
@property(nonatomic,copy)callback cb;
@end
//页面二的.m文件
import "SegueJumpViewController.h"
@interface SegueJumpViewController ()
@end
@implementation SegueJumpViewController
- (IBAction)goBack:(id)sender {
// [self.navigationController popViewControllerAnimated:YES];
NSString * msg = self.cb(@"segueCallBackMsg");
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"%@",msg);
}];
}
2.3 Navigation跳转
/*在AppDelegate.h文件中声明*/
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
//定义一个全局变量
UINavigationController *nav;
}
@property (strong, nonatomic) UIWindow *window;
@end
/*在AppDelegate.m文件中定义根视图和添加导航控制器*/
//获取Main story
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//通过ID获取故事板的UIViewController
ViewController *vc = [story instantiateViewControllerWithIdentifier:@"rootvc"];
//初始化Window对象
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
//初始化根视图
nav = [[UINavigationController alloc]initWithRootViewController:vc];
nav.navigationBarHidden = false;
[nav.navigationController.navigationItem.backBarButtonItem setStyle:UIBarButtonItemStylePlain];
//设置根视图
self.window.rootViewController = nav;
//让当前UIWindow变成keyWindow(主窗口)
[self.window makeKeyWindow];
//让当前UIWindow变成keyWindow,并显示出来
// [self.window makeKeyAndVisible];
/*页面一push跳转*/
//获取要跳转的页面
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
SegueJumpViewController *sgvc = [story instantiateViewControllerWithIdentifier:@"sgvc"];
//执行跳转
[self.navigationController pushViewController:sgvc animated:YES];
/*页面二通过pop返回*/
[self.navigationController popViewControllerAnimated:YES];
- 非常详细的 navigationController 的使用
- iOS UINavigationController里的push和pop操作、抽屉效果原理
- UINavigationController使用详解
2.4Tab
//获取storybroad中UITabBarController控制器
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
tabViewController *tab = [story instantiateViewControllerWithIdentifier:@"tabRootView"];
//设置根视图
[tab.view.window makeKeyWindow];
//选择对应的跳转方式
// [self.navigationController pushViewController:tab animated:YES];
[self presentViewController:tab animated:YES completion:^{
}];