In my application, I have two View Controllers – a UIViewController and UITableViewController.
Now, in my TableViewController implementation I need to retrieve some data from the UIViewController (a property – array). The array it’s synthesized, so what I’m actually doing is that in the implementation of the UITableViewController, I’m creating a instance of my UIViewController, an array, and just set the array to [myUIViewController getTheArray]. I know it won’t work this way, it’s just retrieving nil, but in my VC it’s actually full of data. I am stuck at this point, I could’ve tried with the performSegueWithIdentifier method, but these views are not directly connected via a segue to each other. They are embedded in a Tab Bar View, and my ViewController has absolutely nothing to do with my TableViewController.
Any idea on how to make this work properly?
Thanks.
@interface classA : UIViewController
@property (strong, nonatomic) Playlist* playlist;
-(Playlist*) getTheCurrentPlaylist;
@end
@implementation classA
@synthesize playlist = _playlist;
// add data to _playlist
-(Playlist*) getTheCurrentPlaylist{
return _playlist;
}
@end
now, to classB
@interface classB : UITableViewController
@property (strong ,nonatomic) Playlist* playlistTab;
@end
@implementation classB
@synthesize playlistTab = _playlistTab;
-(void) viewDidLoad{
[super viewDidLoad];
classA *cla = [[classA alloc] init];
// what has to be done here?
_playlistTab = [cla getTheCurrentPlaylist];
}
I hope this makes it clear.
I’m not a 100% sure I understand what you mean so please do comment if I misunderstood something. Also, I’m fairly new to iOS development myself.
Ok, so you have a Tab bar controller with a UIViewController and a UITableViewController. Your implementation of a UIViewController has some data and your UITableViewController needs access to that data. You can’t just create a new classA. You need to refer to the instance of the classA that you created and put into the tab bar.
One thing you can do is to have the AppDelegate be aware of the UIViewController (i.e. AppDelegate has a property that references the UIViewController. Then in you UITableViewController, you can do something like this to access the UIViewController:
Sounds like a real simple application so probably using something like Core Data might be overly complicated. Another way you can do above, is to have your instance of ClassA set a values of some singleton then have ClassB read from that. Or simply just create a property of your AppDelegate that gets written to by UIViewController and read from your table view.