iOS various value-transfer methods

Source: Internet
Author: User
Tags uicontrol uikit

The property pass value passes the information owned by the a page through the property to the B page using

The b page defines a Navititle property that, in a page, passes the value of the a page directly to the B page by assigning a value to the property.

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface rootviewcontroller:uiviewcontroller<changedelegate>

{

Uitextfield *TF;

}

@end

A ROOTVIEWCONTROLLER.M Page Implementation file

#import "RootViewController.h"

#import "DetailViewController.h"

@interface Rootviewcontroller ()

@end

@implementation Rootviewcontroller

Core code

-(void) Loadview

{

UIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect];

Btn.frame = CGRectMake (0, 0, 100, 30);

[Btn settitle:@ "Push" forstate:0];

[Btn addtarget:self Action: @selector (pushaction:) forcontrolevents:uicontroleventtouchupinside];

[Self.view ADDSUBVIEW:BTN];

}

-(void) Pushaction: (ID) sender

{

TF = (Uitextfield *) [self.viewviewwithtag:1000];

Navigating the push to the next page

Pushviewcontroller into the stack reference count +1, and the control right to the system

Detailviewcontroller *detailviewcontroller = [[Detailviewcontrolleralloc]init];

attribute value, direct property assignment

Detailviewcontroller.navititle =tf.text;

Navigating the push to the next page

[Self.navigationControllerpushViewController:detailViewController Animated:yes];

[Detailviewcontrollerrelease];

}

b page DetailViewController.h file

#import <UIKit/UIKit.h>

@interface Detailviewcontroller:uiviewcontroller

{

Uitextfield *textfield;

NSString *_navititle;

}

@property (Nonatomic,retain) NSString *navititle;

@end

b page. m implementation file

#import "DetailViewController.h"

@interface Detailviewcontroller ()

@end

@implementation Detailviewcontroller

@synthesize Navititle =_navititle;

-(void) Loadview

{

Self.view = [[[Uiviewalloc]initwithframe:cgrectmake (0,0, 320,480)]autorelease];

Self.title = Self.navititle;

}

Proxy pass-through value

A page push to page B, if the information on page B wants to return (callback) to a page, using the proxy value, where B defines the protocol and the declaration agent, a to confirm and implement the agent, a as a proxy for B

A page RootViewController.h file

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface rootviewcontroller:uiviewcontroller<changedelegate>

{

Uitextfield *TF;

}

@end

A page ROOTVIEWCONTROLLER.M implementation file

#import "RootViewController.h"

#import "DetailViewController.h"

@interface Rootviewcontroller ()

@end

@implementation Rootviewcontroller

Core code

-(void) Loadview

{

UIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect];

Btn.frame = CGRectMake (0, 0, 100, 30);

[Btn settitle:@ "Push" forstate:0];

A page push to page b

[Btn addtarget:self Action: @selector (pushaction:) forcontrolevents:uicontroleventtouchupinside];

[Self.view ADDSUBVIEW:BTN];

}

-(void) Pushaction: (ID) sender

{

TF = (Uitextfield *) [Self.view viewwithtag:1000];

Navigating the push to the next page

Pushviewcontroller into the stack reference count +1, and the control right to the system

Detailviewcontroller *detailviewcontroller = [[Detailviewcontroller alloc]init];

Proxy pass-through value

Detailviewcontroller.delegate =self;//to make himself an agent.

Navigating the push to the next page

[Self.navigationcontroller Pushviewcontroller:detailviewcontroller Animated:yes];

[Detailviewcontroller release];

}

Implementing Proxy methods

-(void) Changetitle: (NSString *) aStr

{

TF = (Uitextfield *) [Self.view viewwithtag:1000];

Tf.text = astr;//Assigns the parameters passed from page B to the TextField in page a

Tf.text = ASTR;

}

b page detailviewcontroller.m file

#import <UIKit/UIKit.h>

@interface Detailviewcontroller:uiviewcontroller

{

Uitextfield *textfield;

Defining proxies

id<changedelegate>_delegate;

}

@property (nonatomic,assign) id<changedelegate> delegate;

@end

Defining protocols

@protocol changedelegate <NSObject>

-(void) Changetitle: (NSString *) astr;//protocol method

@end

b page DetailViewController.h implementation file

#import "DetailViewController.h"

@interface Detailviewcontroller ()

@end

@implementation Detailviewcontroller

-(void) Loadview

{

Self.view = [[[UIView alloc]initwithframe:cgrectmake (0, 0, +, 480)]autorelease];

Uibarbuttonitem *doneitem = [[Uibarbuttonitemalloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemdonetarget: Selfaction: @selector (doneaction:)];

Self.navigationItem.rightBarButtonItem = Doneitem;

[Doneitemrelease];

}

Pop back to a previous page

-(void) Doneaction: (ID) sender

{

if (self.delegate && [self.delegaterespondstoselector: @selector (changetitle:)])//If the agent exists and responds to changetitle this method

{

[Self.delegate ChangeTitle:textField.text];

[self.delegatechangetitle:textfield.text];//the Textfield.text parameter to the Changetitle method let the agent, that is, a page to implement this method

NSLog (@ "%@", self.navigationController.viewControllers);

[Self.navigationControllerpopViewControllerAnimated:YES];

}

}

Block

Several forms of block

No return value
void (^BLOCK1) (void);
Block1 = ^{
NSLog (@ "Bock demo");
};
Block1 ();

int return type
Int (^block2) (void);
Block2 = ^ (void)
{
int a = 1, b = 1;
int c = a+b;
return C;
};

There are parameters returned
Int (^BLOCK3) (int, int) = ^ (int a, int b)
{
int c = a +b;
return C;

};
NSLog (@ "bock=%d", BLOCK3);

Has a return value, has parameters and can modify the block of variables outside the block
static int sum = 10;//__blcik and static keyword or _block int sum = 10
Int (^BLOCK4) (int) =^ (int a)
{
sum=11;
int c = Sum+a; At this point, sum is modifiable, and if no static or _block keyword is added, the variables outside the block cannot be modified.
return C;
};
NSLog (@ "block4=%d", Block4 (4));

Block Pass Value

For example the value of a (ablock) page preacher B (bblock) page

In the A page ABlock.h

@interface ablock:uiviewcontroller<uitableviewdelegate,uitableviewdatasource>
{
UITableView *_tableview;
UILabel *labe;
Uiimageview *imagevies;
}
@end

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
[_tableview Deselectrowatindexpath:indexpath Animated:yes];

Bblcok *bblock = [[Bblcok alloc] Initwithblock:block_copy (^ (NSString *ablock) {
Labe.text = Ablock;
NSLog (@ "%@", Ablock);

})];


Bblock.imgeviews = Imagevies.image;
bblock. String = Labe.text;
[Self.navigationcontroller Pushviewcontroller:bblock Animated:yes];
[Bblock release];
}

In the A page Bblock.h

#import <UIKit/UIKit.h>
typedef void (^myblock) (NSString *);
@interface Bblcok:uiviewcontroller
{
Uiimageview *image;
Uitextfield *afield;
UIButton *abutt;
NSString *_string;
ID _imgeviews;
Myblock Myblock;
}
@property (nonatomic,copy) Myblock Myblock;
@property (nonatomic,retain) ID imgeviews;
@property (Nonatomic,retain) NSString *string;
-(ID) Initwithblock: (Myblock) Ablcok;
@end

//
Bblcok.m
Blcok
//
Created by Zhu on 13-8-12.
Copyright (c) 2013 Zhu Ji Fan. All rights reserved.
//

#import "Bblcok.h"

@interface Bblcok ()

@end

@implementation Bblcok
@synthesize imgeviews = _imgeviews, String = _string;
@synthesize myblock = _myblock;
-(ID) Initwithblock: (Myblock) Ablcok
{
if (self = [super init])
{
Self.myblock = Ablcok;
}
return self;
}

-(void) dealloc
{
[Super Dealloc];
}

-(void) Loadview
{
Uicontrol *cont = [[Uicontrol alloc] Initwithframe:cgrectmake (0, 0, 320, 568-44)];
[Cont addtarget:self action: @selector (Clcik) forcontrolevents:uicontroleventtouchupinside];
Self.view = cont;

afield = [[Uitextfield alloc] Initwithframe:cgrectmake (60, 10, 160, 30)];
Afield.borderstyle = Uitextborderstyleline;
Afield.placeholder = self. String;
[Self.view Addsubview:afield];

Abutt = [UIButton buttonwithtype:uibuttontyperoundedrect];
Abutt.frame = CGRectMake (60, 50, 70, 30);
[Abutt settitle:@ "Modify" forstate:0];
[Abutt addtarget:self Action: @selector (Abuttclcik:) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:abutt];

Image = [[Uiimageview alloc] Initwithframe:cgrectmake (60, 100, 210, 260)];
Image.backgroundcolor = [Uicolor Bluecolor];
Image.image = Self.imgeviews;
[Self.view Addsubview:image];
[Image release];

}

-(Ibaction) Abuttclcik: (ID) sender
{
NSString *sting = Afield.text;
Myblock (Sting);
[Self.navigationcontroller Poptorootviewcontrolleranimated:yes];
}


-(void) Clcik
{
[Afield Resignfirstresponder];
}
-(void) viewdidload
{
[Super Viewdidload];
Do any additional setup after loading the view.
}

-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}

@end

iOS various value-transfer methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.