Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 4    Views: 50

PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
08/20/14 05:28 PM (10 years ago)

Carrying a dynamic NSURL value to another class

This *kind of* follows on from this thread a while back https://www.buzztouch.com/forum/thread.php?tid=2FFEB202D37E99BE156E1F4&sortColumn=FT.id&sortUpDown=DESC&currentPage=1, but I'm trying to share a variable ACROSS classes, not between methods in the same class. I have an NSURL (a lat/long request to php) being sent off and then reloaded onto my map. That lat/long pair is generated on the fly from a user search. Works beautifully and I now effectively have a third dataURL option coded into BT_screen_map. I want to use the exact same NSURL value in another class, which basically loads the same map data but in list form through Susan's "menu with Image" plugin. So my question is how to send the NSURL variable to that second class? I call up that second class with a UISegmentedControl that sits in the first class, so the segmented control needs to be able to send the NSURL value off to the second class to do stuff with. Each time the second class is called, the value of the NSURL will almost definitely be different, so it can't be sent as an absolute value. Any thoughts or pointers? Cheers Paddy
 
allandriggers
Apple Fan
Profile
Posts: 188
Reg: Dec 13, 2012
Knoxville, TN
11,680
like
08/20/14 06:16 PM (10 years ago)
Without really knowing how Susan coded her plugin, your question is hard to answer. But it sounds like you are trying to take a variable from one class and load it into a tableview in another class. It might be easier to create a custom plugin and class, and import the name of the first class into the second class. Lets say you want to display a name and phone number in a table cell based on a search query. Your variable names are name and phoneNumber that you have declared and defined in another class. you would simply call import that class name into your new class like follow and set the new .xib file to the new class name. I have included how i would code something like that. I hope it gives you an idea. #import "yourfirstclassname.h" @interface ResultsTableViewController () @end @implementation ResultsTableViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.clearsSelectionOnViewWillAppear = NO; self.navigationItem.rightBarButtonItem = self.editButtonItem; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { RouteViewController *routeController = [segue destinationViewController]; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; long row = [indexPath row]; routeController.destination = _mapItems[row]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return _mapItems.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"resultCell"; ResultsTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; long row = [indexPath row]; MKMapItem *item = _mapItems[row]; cell.nameLabel.text = item.name; cell.phoneLabel.text = item.phoneNumber; return cell; }
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
08/20/14 06:54 PM (10 years ago)
This might not be the "right way" ... It works! :-) I made a property in the AppDelegate file. Then in Menu plugin, reference that property via the appDelegate. (Going from memory, only have iPhone) -- Niraj
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
08/21/14 03:55 PM (10 years ago)
Thanks for those suggestions ... allandriggers I don't have any xibs so not sure if that will work, but importing the other class file is definitely a start, thanks. Perhaps it's just setting up the declarations etc properly that I'm not doing correctly. Will have a play. Niraj, have you had any luck with your method when the global variable itself has a variable value? I can see how it would work if eg I declared: myAppDelegate.myVariable = 1 then I see how that's available to all other classes, everything knows that it's 1, but if it's: myAppDelegate.myVariable = theFirstViewController.NSURL.myOtherVariable (bad syntax, but you get the idea) then it still has to grab myOtherVariable somehow, I think, which comes back to allandriggers' approach. What do you reckon? Cheers Paddy.
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
08/21/14 08:09 PM (10 years ago)
Within the theFirstViewController class, do a myAppDelegate.myVariable = self.NSURL.myOtherVariable within the viewWillDisappear ( still on iPhone :-) -- Niraj
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.