页面之间的普通传值和反向传值

页面之间的数据传递是最基本的,下面就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实现简单

 

基于遗算法的新的异构分布式系统任务调度算法研究(Matlab代码实现)内容概要:本文档围绕基于遗算法的异构分布式系统任务调度算法展开研究,重点介绍了一种结合遗算法的新颖优化方法,并通过Matlab代码实现验证其在复杂调度问题中的有效性。文中还涵盖了多种智能优化算法在生产调度、经济调度、车间调度、无人机路径规划、微电网优化等领域的应用案例,展示了从理论建模到仿真实现的完整流程。此外,文档系统梳理了智能优化、机器学习、路径规划、电力系统管理等多个科研方向的技术体系与实际应用场景,强调“借力”工具与创新思维在科研中的重要性。; 适合人群:具备一定Matlab编程基础,从事智能优化、自动化、电力系统、控制工程等相关领域研究的研究生及科研人员,尤其适合正在开展调度优化、路径规划或算法改进类课题的研究者; 使用场景及目标:①学习遗算法及其他智能优化算法(如粒子群、蜣螂优化、NSGA等)在任务调度中的设计与实现;②掌握Matlab/Simulink在科研仿真中的综合应用;③获取多领域(如微电网、无人机、车间调度)的算法复现与创新思路; 阅读建议:建议按目录顺序系统浏览,重点关注算法原理与代码实现的对应关系,结合提供的网盘资源下载完整代码进行调试与复现,同时注重从已有案例中提炼可迁移的科研方法与创新路径。
【微电网】【创新点】基于非支配排序的蜣螂优化算法NSDBO求解微电网多目标优化调度研究(Matlab代码实现)内容概要:本文提出了一种基于非支配排序的蜣螂优化算法(NSDBO),用于求解微电网多目标优化调度问题。该方法结合非支配排序机制,提升了统蜣螂优化算法在处理多目标问题时的收敛性分布性,有效解决了微电网调度中经济成本、碳排放、能源利用率等多个相互冲突目标的优化难题。研究构建了包含风、光、储能等多种分布式能源的微电网模型,并通过Matlab代码实现算法仿真,验证了NSDBO在寻找帕累托最优解集方面的优越性能,相较于其他多目标优化算法表现出更强的搜索能力稳定性。; 适合人群:具备一定电力系统或优化算法基础,从事新能源、微电网、智能优化等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于微电网能量管理系统的多目标优化调度设计;②作为新型智能优化算法的研究与改进基础,用于解决复杂的多目标工程优化问题;③帮助理解非支配排序机制在进化算法中的集成方法及其在实际系统中的仿真实现。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注非支配排序、拥挤度计算蜣螂行为模拟的结合方式,并可通过替换目标函数或系统参数进行扩展实验,以掌握算法的适应性与调参技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值