Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
patrick tagliaferro
5,399 Pointsbutton with url link from variable
So I am getting a url link from parse in a method and want to pass the url to a button. I am getting the url fine but when I use the variable in the button I am getting an "Undeclared Identifier" since I defined the variable in the prior method I image. How do I get this to work??
- (void)receivedSighting:(FYXVisit *)visit updateTime:(NSDate *)updateTime RSSI:(NSNumber *)RSSI;
{
NSLog(@"Gimbal Beacon!!! %@", visit.transmitter.name);
// this will be invoked when an authorized transmitter is sighted during an on-going visit
PFQuery *query = [PFQuery queryWithClassName:@"houses"];
[query whereKey:@"name" equalTo:[NSString stringWithFormat:@"%@",visit.transmitter.name]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
NSLog(@"address: %@", [object objectForKey:@"address"]);
NSLog(@"url:: %@", [object objectForKey:@"url"]);
NSString *displayAddress = [NSString stringWithFormat:@"%@", [object objectForKey:@"address"]];
NSString *displayDescription = [NSString stringWithFormat:@"%@", [object objectForKey:@"description"]];
//This is where I was getting the URL for the button
NSString *displayUrl = [NSString stringWithFormat:@"%@", [object objectForKey:@"url"]];
self.addressLabel.text = displayAddress;
self.descLabel.text = displayDescription;
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
- (IBAction)linkButton:(id)sender {
//Pass the URL to the button
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:displayUrl]];
}
1 Answer
Stone Preston
42,016 PointsThe reason you are getting that error is becauase your displayURL variable is out of the scope of that linkButton method. Its only accessible inside the if statement you declared it in here:
if (!error) {
for (PFObject *object in objects) {
NSLog(@"address: %@", [object objectForKey:@"address"]);
NSLog(@"url:: %@", [object objectForKey:@"url"]);
NSString *displayAddress = [NSString stringWithFormat:@"%@", [object objectForKey:@"address"]];
NSString *displayDescription = [NSString stringWithFormat:@"%@", [object objectForKey:@"description"]];
//This is where I was getting the URL for the button
NSString *displayUrl = [NSString stringWithFormat:@"%@", [object objectForKey:@"url"]];
self.addressLabel.text = displayAddress;
self.descLabel.text = displayDescription;
}
What you need to do is make a property or instance variable to hold the URL so its accessible throughout your view controller and then instead of creating a new string to hold the URL in that if statement, just assign it to the property like so:
//This is where I was getting the URL for the button
self.displayUrl = [NSString stringWithFormat:@"%@", [object objectForKey:@"url"]];
then you will be able to reference it in your IBAction:
- (IBAction)linkButton:(id)sender {
//Pass the URL to the button
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.displayUrl]];
}
patrick tagliaferro
5,399 Pointspatrick tagliaferro
5,399 PointsThanks. So anytime I need to define a variable within a method and use it outside that method just make it a property, in this case just
@property (nonatomic) NSString *displayUrl;Stone Preston
42,016 PointsStone Preston
42,016 Pointsyes thats correct.