iOS block resolves circular references and callback values
Last Update:2015-07-07
Source: Internet
Author: User
<span id="Label3"></p><p><p>There is a need to customize the view control in order to reduce the coupling of the controller, but now another page needs to display the values on the custom view; a block callback to the controller is required to display</p></p><p><p>A lot of talk, a simple and clear (demand: B controller to the a controller to pass the value).</p></p><p><p>1. First step is to define the block in the B Controller for Example:</p></p><p class="p1"><p class="p1"><span class="s1">#import <span class="s2">"BViewController.h"</span></span></p></p><p class="p2"><p class="p2"><span class="s3">typedef<span class="s2"> <span class="s3">void<span class="s2"> (^ answerblock) (<span class="s4">nsstring<span class="s2"> *resutlstr);</span> </span> </span> </span> </span></span></p></p><p class="p3"><p class="p3"><span class="s3">@class<span class="s5"> <span class="s2">gxriskrelatedquery<span class="s5">;</span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s3">@interface<span class="s2"> gxriskviewcontroller: <span class="s6">bviewcontroller</span> </span> </span> <span class="s2"> </span></p></p><p class="p5"><p class="p5"><span class="s2">@property<span class="s5"> (<span class="s2">nonatomic<span class="s5">, <span class="s2">copy<span class="s5">) <span class="s6">answerblock<span class="s5"> answerblock;</span> </span> </span> </span> </span> </span> </span></span></p></p><p class="p4"><p class="p4"><span class="s2">@end</span></p></p><p class="p4"><p class="p4"><span class="s2">2. In The. m file of the B controller, (callback is the network data, so it should be implemented in the network request of the B controller where this block is Successful)</span></p></p><p class="p1"><p class="p1"><span class="s1">#pragma mark <span class="s2">submits the risk assessment answer interface</span></span></p></p><p class="p2"><p class="p2"><span class="s1">-(<span class="s3">void<span class="s1">) requestdatawithrisksubmit: (<span class="s4">nsstring<span class="s1"> *) answerstring</span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1">{</span></p></p><p class="p2"><p class="p2"><span class="s1"><span class="s4">nsdictionary<span class="s1"> *params = <span class="s5">@{<span class="s6">@ "fundid"<span class="s1"> : [[<span class="s7">gxsharedclass<span class="s1"> <span class="s8">sharedclass<span class="s1">] <span class="s8">gxaccount <span class="s1">],</span></span></span></span></span></span></span></span></span></span></span></span></p></p><p class="p2"><p class="p2"><span class="s1"><span class="s6">@ "answers"<span class="s1">: answerstring<span class="s5">}<span class="s1">;</span> </span> </span> </span></span></p></p><p class="p3"><p class="p3"><span class="s9"><span class="s10">mjlog<span class="s9">(<span class="s1">@ "params = =%@"<span class="s9">, params);</span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1">[<span class="s7">gxrisksubmit<span class="s1"> <span class="s8">requestwithparams<span class="s1">:p arams <span class="s8">result<span class="s1">: ^ (<span class="s4">nsinteger<span class="s1"> respcode, <span class="s4">nsstring <span class="s1">*respmsg, <span class="s3">ID<span class="s1"> Responseobject) {</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1"><span class="s3">if<span class="s1"> (respcode = = <span class="s5">1<span class="s1">) {</span> </span> </span> </span></span></p></p><p class="p4"><p class="p4"><span class="s1"></span></p></p><p class="p2"><p class="p2"><span class="s1"><span class="s7">gxrisksubmit<span class="s1"> *risksubmit = (<span class="s7">gxrisksubmit<span class="s1"> *) responseobject[<span class="s5">0<span class="s1">];</span> </span> </span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1"><span class="s3">self<span class="s1">. <span class="s7"> Messagestr<span class="s1"> = Risksubmit. <span class="s7"> Message<span class="s1">;</span> </span> </span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1">[<span class="s3">self<span class="s1"> <span class="s8">setupalertview<span class="s1">];</span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1"><span class="s3"><span class="s1"><span class="s8"><span class="s1">This is about judging the model for Data.</span></span></span></span></span></p></p><p class="p2"><p class="p2"><span class="s1"><span class="s3">if<span class="s1"> (risksubmit. <span class="s7"> Ishavedata<span class="s1">) {</span> </span> </span> </span></span></p></p><p class="p4"><p class="p4"><span class="s1"> </span> <span class="s1"> <span class="s3">if<span class="s1"> (<span class="s3">self<span class="s1">. <span class="s7"> Answerblock<span class="s1">) {</span> </span> </span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1"><span class="s3">self<span class="s1">. <span class="s7"> Answerblock<span class="s1">(risksubmit. <span class="s7"> Risktype<span class="s1">);//this is the value of the callback, which is the value obtained from the model, that is, the value to be displayed in the A controller</span> </span> </span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1">}</span></p></p><p class="p4"><p class="p4"><span class="s1"></span></p></p><p class="p2"><p class="p2"><span class="s1">}</span></p></p><p class="p4"><p class="p4"><span class="s1"></span></p></p><p class="p2"><p class="p2"><span class="s1">}</span></p></p><p class="p2"><p class="p2"><span class="s1">}];</span></p></p><p class="p4"><p class="p4"><span class="s1"></span></p></p><p class="p2"><p class="p2"><span class="s1">}</span></p></p><p class="p2"><p class="p2"><span class="s1">3. Then come to the a controller to implement the following methods</span></p></p><p class="p2"><p class="p2"><span class="s1">The first thing you need to know is where a controller is push to the B Controller (i wrote the project I know where the push Is)</span></p></p><p class="p1"><p class="p1"><span class="s1"><span class="s2">b Controller *BVC = [[b Controller <span class="s1"> <span class="s2"> <span class="s3">alloc<span class="s2">] <span class="s3">init<span class="s2">];</span> </span> </span> </span> </span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s4">__weak<span class="s2"> a controller <span class="s1"><span class="s2">*__self = <span class="s4">self<span class="s2">; This code is to resolve a circular reference</span> </span> </span></span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1">Riskviewvc. <span class="s5">Answerblock<span class="s1"> = ^ (<span class="s6">nsstring<span class="s1"> *answer) {</span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1">[__self <span class="s7">receiveanwser<span class="s1">: answer];</span> </span></span></p></p><p class="p2"><p class="p2"><span class="s1">};</span></p></p><p class="p2"><p class="p2"><span class="s1">[<span class="s4">self<span class="s1">. <span class="s6"> Navigationcontroller<span class="s1"> <span class="s3">pushviewcontroller<span class="s1">: BVC <span class="s3">animated<span class="s1">:<span class="s4">YES<span class="s1">];</span> </span> </span> </span> </span> </span> </span></span></span></span></span></p></p><p class="p2"><p class="p2"></p></p><p class="p2"><p class="p2"><span class="s1">4. finally, you have to implement a method (this method is used to assign a Value)</span></p></p><p class="p1"><p class="p1"><span class="s1">-(<span class="s2">void<span class="s1">) receiveanwser: (<span class="s3">nsstring<span class="s1"> *) resultstr</span> </span> </span> </span></span></p></p><p class="p1"><p class="p1"><span class="s1">{</span></p></p><p class="p1"><p class="p1"><span class="s1"><span class="s4">_guardlabel<span class="s1">. <span class="s3"> Text<span class="s1"> = resultstr;</span> </span> </span> </span></span></p></p><p class="p2"><p class="p2"><span class="s5">[<span class="s1">gxsharedclass<span class="s5"> <span class="s6">sharedclass<span class="s5">]. <span class="s1"> Riskvalue<span class="s5"> = resultstr;</span> </span> </span> </span> </span> </span></span></p></p><p class="p1"><p class="p1"><span class="s1">}</span></p></p><p class="p2"><p class="p2"><span class="s1"></span></p></p><p><p>iOS block resolves circular references and callback values</p></p></span>