Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As @trojanfoe said, delegation is a better way to do it. Instead of <code>#import "RootViewController.h"</code>, it is better to adop delegation. Because <code>UITableViewCell</code> is a child and <code>RootViewController</code> is the parent view. You don't want the child to talk <strong>directly</strong> with the parent.</p> <ul> <li><p>To adopt delegation:</p></li> <li><p>in <code>MyCell.h</code> file</p></li> <li><p>remove <code>#import "RootViewController.h"</code>.</p></li> <li><p>revise <code>MyCell.h</code> as follows:</p></li> </ul> <blockquote> <pre><code>@protocol MyCellDelegate; // if you need to have forward declaration @interface MyCell : UITableViewCell // @property (nonatomic, strong) IBOutlet RootViewController *rootViewController; @property (nonatomic) id &lt;MyCellDelegate&gt; delegate; @end @protocol MyCellDelegate &lt;NSObject&gt; - (void)refreshVisibleViewForCellTagged:(NSInteger)cellTag; @end </code></pre> </blockquote> <ul> <li>in <code>MyCell.m</code>.</li> </ul> <blockquote> <pre><code>@synthesize delegate; -(IBAction)checkBoxClicked:(UIButton*)sender { [self setCheckBoxChecked:!_checkBoxChecked]; //[_rootViewController refreshVisibleViewForCellTagged:self.tag]; [self.delegate refreshVisibleViewForCellTagged:self.tag]; } </code></pre> </blockquote> <ul> <li>in <code>RootViewController.h</code> adopt the delegation of MyCell</li> </ul> <blockquote> <pre><code>#import "MyCell.h" @interface RootViewController : UIViewController &lt;MyCellDelegate&gt; </code></pre> </blockquote> <ul> <li>in <code>RootViewController.m</code>.</li> </ul> <blockquote> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = // your implementation //assuming all your cells are of MyCell kind // set RootViewController as the delegate of each cell ((MyCell *)cell).delegate = self; return cell; } </code></pre> </blockquote> <ul> <li>implement the delegate method in <code>RootViewController.m</code>.</li> </ul> <blockquote> <pre><code>- (void)refreshVisibleViewForCellTagged:(NSInteger)cellTag { // whatever you have } </code></pre> </blockquote> <p>P.S. The above codes are for illustration. I didn't run them. If some part doesn't work, let me know, and I'll revise it.</p> <p>The reason those objects in RootViewController are nil in the way you call, is because you are not accessing the same instance of RootViewController. It is a different (new) instance and hence all objects are nil. </p>
 

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