Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>in general if you want an object to store/return information that it doesn't usually store based on its existing properties instance variables, you can use objective-c's runtime <a href="https://developer.apple.com/library/ios/documentation/cocoa/reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/c/func/objc_setAssociatedObject" rel="nofollow">object association</a>. </p> <p>So consider this category:</p> <pre><code>#import "NSObject+Addons.h" #import &lt;objc/runtime.h&gt; @implementation NSObject (Addons) static char infoKey; -(id)info { return objc_getAssociatedObject(self, &amp;infoKey); } -(void)setInfo:(id)info { objc_setAssociatedObject(self, &amp;infoKey, info, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } </code></pre> <p>now suppose that we are in <code>viewController1</code> and it makes a specific request.. so you can put this code</p> <pre><code>//viewController1.m // make specific request // now store in UINavBar name of this viewcontroller // (you can use an enum here instead if you like) [self.navigationController setInfo@"viewController1"]; </code></pre> <p>so later on in your code, when you want to return the data back to the return controller that made that specific request (you didn't say exactly where).. you can check the value of info:</p> <pre><code>if ([[navigationController info] isEqualToString:@"viewController1"]]) { //do stuff } else if ([[navigationController info] isEqualToString:@"viewController2"]]) { // etc.. </code></pre> <p>this is the quick solution.. however if it was my own code.. I would use the <a href="http://en.wikipedia.org/wiki/Mediator_pattern" rel="nofollow">mediator pattern</a>.. basically create a controller of controllers (let's just call it controller), and make it a singelton. This object would keep the in-between info such as which viewcontroller did what, and return that information whenever necessary.. in a way the <code>UINavigationBar</code> is doing this in the code above, but we're using runtime patchwork code to make it happen. Using the controller of controllers is more robust and the concept can cover many other useful tasks that come up for most projects. The idea is design code that is closed for modification but open for extension. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload