Using block-pass values in iOS

Source: Internet
Author: User

Using this method to pass a value overrides the delegate. Concrete Example MainView.h

#import <UIKit/UIKit.h>


@interface Mainview:uiviewcontroller

{

Iboutlet uibutton* btn;

Iboutlet uilabel* labshow;

}

-(Ibaction) Push: (ID) sender;

@end


mainview.m

#import "MainView.h"

#import "SecondView.h"


@implementation Mainview


-(ID) Initwithnibname: (nsstring*) Nibnameornil Bundle: (nsbundle*) Nibbundleornil

{

self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];

if (self) {

}

return self;

}


-(void) didreceivememorywarning

{

[Superdidreceivememorywarning];

}


#pragma mark-view lifecycle


-(void) viewdidload

{

[Superviewdidload];

}

-(Ibaction) Push: (ID) sender

{

Secondview *s =[[secondview alloc] initwithblock:block_copy (^ (NSString *str) {

NSLog (@ "%@", str);

Labshow.text = str;

})];

[Self.navigationcontroller pushviewcontroller:s Animated:yes];

[s release];

}

-(void) viewdidunload

{

[Superviewdidunload];

}


-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation

{

return (interfaceorientation = = uiinterfaceorientationportrait);

}


@end


Second.h

#import <UIKit/UIKit.h>

typedef void (^myblock) (nsstring*);


@interface Secondview:uiviewcontroller

{

Iboutlet Uitextfield*txtview;

Myblock my;

}

-(Ibaction) Back: (ID) sender;

-(ID) Initwithblock: (Myblock) str;

@end


Second.m

#import "SecondView.h"


@implementation Secondview


-(ID) Initwithblock: (myblock) str

{

self = [super init];

if (self)

{

My =str;

}

return self;

}

-(Ibaction) Back: (ID) sender

{

nsstring* s = txtview.text;

if (my)

{

my (s);

}

[Self.navigationControllerpopViewControllerAnimated:YES];

}


-(void) didreceivememorywarning

{

[Superdidreceivememorywarning];

}

-(void) dealloc{

Block_release (my);

[Super Dealloc];

}

#pragma mark-view lifecycle


-(void) viewdidload

{

[Superviewdidload];

}


-(void) viewdidunload

{

[Superviewdidunload];

}


-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation

{

return (interfaceorientation = = uiinterfaceorientationportrait);

}


@end

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.