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
Lyubomir Marinov
940 PointsiOS: How to access already declared pointer in another view?
Here's the thing: I am using Mapbox iOS SDK to build a map in a UIViewController. I also have a sliding menu with a UITableViewController and some cells for navigation (like Facebook app in it's previous versions), but when I tap a cell, I can't get access to that Mapbox *pointer with the map itself. I tried with with a new project. Single view application. Two UIViewControllers for each part (sliding menu and main view), tried to use IBOutlets (pair of UIButton pointers) in both views and access the IBOutlet on the main view inside the sliding menu UIViewController - nothing. I couldn't even change the title of the button after I called the UIViewController. So, please. Tell me what is the magic - I want to declare the button/map/UIView/etc. in a UIViewController 1, and then I want to have access to it inside UIViewController 2. Thank you in advance, God bless you all!
5 Answers
Vladimir Cezar
7,710 PointsThe trick is to UIViewController2 as a delegate UIViewController1.
Assume that there is a Navigation Controller and 2 views, VC1 and VC2, connected by a segue and I want a button on VC2 to change a label on VC1. Here is how it would work.
View Controller 1 (header)
@interface VC1 : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *label;
@end
View Controller 1 (implementation)
#import "VC1.h"
#import "VC2.h"
@interface VC1 () <VC2Delegate>
@end
@implementation VC1
- (void)viewDidLoad{
[super viewDidLoad];
}
- (void)changeMyLabel:(NSString *)text{
self.label.text = text;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
VC2 *vc2 = (VC2 *) [segue destinationViewController];
vc2.delegate = self;
}
View Controller 2 (header)
@protocol VC2Delegate <NSObject>
@required
- (void) changeMyLabel:(NSString *)text;
@end
@interface VC2 : UIViewController
@property (assign, nonatomic) id <VC2Delegate> delegate;
- (IBAction)changeSomething:(id)sender;
@end
View Controller 2 (implementation)
#import "VC2.h"
@interface VC2 ()
@end
@implementation VC2
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)changeSomething:(id)sender {
[_delegate changeMyLabel:@"Changed!"];
}
@end
Lyubomir Marinov
940 PointsI'd love to, but I tried the code and somehow it didn't work. So I uploaded my code to github, and if you want, you can check it out :) Thanks in advance
Vladimir Cezar
7,710 PointsYour code does not build.
Vladimir Cezar
7,710 PointsYou forgot to copy the library you are trying to use. Hint: Use CocoaPod to do that.
Lyubomir Marinov
940 PointsSorry :( I'm new to XCode programming and I didn't know XCode didn't copy the files to the project directory. Not I think everything should be fixed. Sorry once again.
Vladimir Cezar
7,710 PointsEverybody was a beginner once. Here, take a look at my example code on Github: https://github.com/vladimircezar/nada
Lyubomir Marinov
940 PointsYep, it works perfectly :) So I guess my problem is with the sliding menu class itself .... :(
Vladimir Cezar
7,710 PointsIt may not. See if you can make your main view controller the delegate of your sliding class.
Lyubomir Marinov
940 PointsI found my solution - I used singletons. This is sample Singleton.h file (I wrote more complex, this is just for the newbies like me, and please, correct me, if I'm wrong)
@interface Singleton : NSObject {
NSString *someString;
}
+(Singleton *)getInstance;
-(NSString *)getTheString;
-(void)setTheString:(NSString *)newString;
@end
and the implementation file looks like this:
#import "Singleton.h"
@implementation Singleton
static Singleton *singletonInstance;
+(Singleton *)getInstance {
if(singletonInstance == nil) {
singletonInstance = [[super alloc] init];
}
return singletonInstance;
}
-(NSString *)getTheString {
return someString;
}
-(void)setTheString: (NSString *)newString {
someString = newString;
}
@end
And then in my ViewController.m I add the following:
#import "Singleton.h"
:
:
Singleton *mySingleton = [Singleton getInstance];
[mySingleton setTheString:@"Hello, world"];
NSLog(@"the string is: %@", [mySingleton getTheString]);
I guess this is not the best way to use one certain object within the whole application, but for the moment this is what saved my day :)
Cheers.