Shun Chuan
Suppose A is the first view controller and B is the second view controller
Import the. h file of B in a
Scenario: A to B pass value
First step: Define a content property in the. h of B
@interfaceSecondViewController: [Email protected] (nonatomic,copy) nsstring*contents; @end
Second step: The content property of B is assigned by clicking the button method in a
-(void) Buttonaction: (uibutton*) button {NSLog (@ "Enter second page"); Secondviewcontroller *SECONDVC = [Secondviewcontroller alloc] init]; Secondvc.contents =self.label.text; [Self.navigationcontroller PUSHVIEWCONTROLLER:SECONDVC Animated:yes]; }
Part III: Assigning a value to the corresponding control using the properties of content in B
@implemention secondviewcontroller-(void) Viewdidload {[Superviewdidload];self.view.backgroundcolor = [ Uicolorwhitecolor];self.navigationitem.title =self.contents; }
Reverse transmission
The proxy value is passed from the forward value after two interfaces.
Suppose A is the first view controller and B is the second view controller
Scenario: B-to-a-pass value
First step: Declare protocol and Protocol methods in the. h file of B first
Step two: Declare a proxy property in the. h of B, where the main note is assign or weak, weak and assign are pointers to non-owning relationships, and pointer variables decorated with both modifiers do not alter the reference count of the referenced object. But after an object is freed, weak automatically points the pointer to nil, and assign does not. So it's safer to use weak.
@property (nonatomic,weak) id< Agreement name >delegate;
#pragma mark here is B's. H#[email protected]//BTN's tag out of the way-(void) Selectedindexwithtag: (Nsinteger) Tag;@[email protected ]:uiview//declares a proxy property [email protected] (nonatomic,weak) iddelegate; @end
Part III: When B is about to pop back to the previous interface, the last line of the Pop method uses the protocol method to pass the data [Self.delegate protocol Method name: (parameter, that is, the data to be returned)
#pragma mark here is B's. m//determines whether the protocol method is implemented in the established proxy class//ensures that there is no crash if this method is executed without crashing if ([Self.delegate respondstoselector: @selector ( Selectedindexwithtag:)] {//execute proxy method [Self.delegate Selectedindexwithtag: (sender.tag-1000)];} Else{nslog (@ "The method in the protocol is not implemented");}
In the. m of a, before the push to the B interface method, after the initialization of the B object, specify the A object as a proxy (B object) for the B object. Delegate = Self There will be a yellow warning, because there is no quasi-protocol
#pragmamark A. M medium//designated agent, B is customviewcustomview. delegate=self;
Fifth: Import the Protocol name < protocol name in the extension of a or a. h file >
#pragma mark A's. m extension, A is the roottabbarcontroller//protocol import @interfaceroottabbarcontroller () @end
The sixth step: in A. m in the event protocol method, obtained in the parameters, presented in the current interface
#pragma mark A's. m//implements the Proxy method, here you can use the value from B-(void) Selectedindexwithtag: (nsingeter) tag {self.selectedindex = tag;}
Use the block page to transfer values between
Step one: Redefine a block in the. h of B and declare the properties of a class with this redefined block type notice that the Block property is decorated with copy
#pragma mark B's. h #import//Block Pass value//rename a block type typedefvoid (^passvalue) with no return value (Nsintegertag); @ interfacecustomtabbarview:uiview//uses this block type to define an attribute @property (nonatomic,copy) passvalue Passvaluetag; @end
Step two: The method of calling block in the return method of the. M of B
#pragmamark B in the return method of the. m//Call block method Self.passvaluetag (sender.tag-1000);
Step three: In the. m of a, where you create an instance of B, assign a value to the Block property of B, that is, write the contents of this block, similar to assigning an initial value to a property of B.
Set block Contents Customview.passvaluetag = ^ (nsintegertag) {self.selectedindex = tag; };
Block memory that does not reference a local variable is stored in the global zone
Block memory referencing local variables is stored in the stack area
Block memory is in heap when copy operation is performed on block
Circular reference problem for block
When block is a property of self
Self.circleblock = ^ () {My_self.navigationItem.title = @ "Hello";};
will result in self's reference count of +1, resulting in a circular reference
Use weak modifier variables under ARC to prevent circular references
Use block modifier variables to prevent circular references under non-arc
IOS Proxy Protocol Transfer value