viewDidLoad not running when expected - objective-c

I have the following code in my viewDidLoad to change an image:
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"colour"] == nil) {
[defaults setObject:#"Red" forKey:#"colour"];
[defaults setInteger:0 forKey:#"colourIndex"];
[defaults synchronize];
}
[background setImage:[UIImage imageNamed:[defaults objectForKey:#"colour"]]];
NSLog(#"%#", [defaults objectForKey:#"colour"]);
// Logs "Blue"
}
When I open my flipside view in the iPhone Simulator and then go back to my original view, the result of the NSLog doesn't change and the image doesn't change. Why is this happening?

Move this code to viewWillAppear. viewDidLoad is not called every time the view is presented.
This link provides a good explanation.
UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?

Related

I want to stay on logout page, when application runs again (if user did not hit logout button)

Below is my code till sign in process
#import "ViewController.h"
#import "LogOutViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userid = [defaults objectForKey:#"uid"];
NSString *password = [defaults objectForKey:#"pswrd"];
_lbluserid.text = userid;
_lblpswrd.text = password;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnsignin:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_lbluserid.text forKey:#"uid"];
[defaults setObject: _lblpswrd.text forKey:#"pswrd"];
[defaults synchronize];
NSLog(#"Credentials are saved");
LogOutViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"logoutvc"];
controller.getUserid = _lbluserid.text;
[self.navigationController pushViewController:controller animated:YES];
}
#end
I have another view which will appear by signing in, on that view I have Log Out button, Now what I want is if user didn't hit logout button the same page (log out page) will be displayed when I run my app again.
Please help me..
try amend you code to.
#import "ViewController.h"
#import "LogOutViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userid = [defaults objectForKey:#"uid"];
NSString *password = [defaults objectForKey:#"pswrd"];
_lbluserid.text = userid;
_lblpswrd.text = password;
if([defaults boolForKey:#"logedIn"]){
[self moveToLogoutViewController];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnsignin:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_lbluserid.text forKey:#"uid"];
[defaults setObject: _lblpswrd.text forKey:#"pswrd"];
[defaults setBool: YES forKey:#"logedIn"];
[defaults synchronize];
NSLog(#"Credentials are saved");
[self moveToLogoutViewController];
}
- (void) moveToLogoutViewController{
LogOutViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"logoutvc"];
controller.getUserid = _lbluserid.text;
[self.navigationController pushViewController:controller animated:YES];
}
#end
You can try like this
First Open AppDelegate class in didFinishLaunchingWithOptions method.
Check user is exist or not by using NSUserDefaults.If user exist you can show LogOutViewController
Use keychain to store user credentials. In your viewCOntroller class, in viewDidLoad check if user credentials exist or not.If exist, then call:
LogOutViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"logoutvc"];
controller.getUserid = //get user id from keychain;
[self.navigationController pushViewController:controller animated:YES];

how to align an image in UISlider?

I change the swipe of a UISlider button for a picture. the problem is that was out of place, how can I solve this?
link img -> http://i.imgur.com/5L5IsB6.png
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
zoomValue = [[defaults objectForKey:#"music.zoom.value"] floatValue];
self.slider.value = zoomValue;
[self.slider setThumbImage:[UIImage imageNamed:#"btn-tamanho-fonte"] forState:UIControlStateNormal];
}
Subclass UISlider and override thumbRectForBounds:trackRect:value:.

Getting Full Name from Facebook from a UIViewController

Hello so I am just starting out with the Facebook iOS SDK and I can sign in to Facebook easy but I am having problems figuring out how to get the users full name and displaying it in a UILabel. Also I am doing this from a UIViewController and so far everything I have seen on the internet has taken place in the app delegate, however, I don't want it to be done from the delegate, it just is not the right place to do this i want it in a UIViewController so when a button is pressed you can sign in to Facebook. The Facebook SDK just seems complicated right now as its my first time using it.
EDIT:
okay heres some of my code.
This is in the view controller:
-(void)createfbInstance {
Facebook* fb = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
self.facebook = fb;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray* permissions = [[NSArray alloc] initWithObjects:#"user_about_me", nil];
[facebook authorize:permissions];
}
}
So I just set it up like the Facebook tutorial
Then I try to send a request with graph path:
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
FBAppDelegate* appDelegate = (FBAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.facebook requestWithGraphPath:#"name" andDelegate:self];
}
Then try and receive the request:
- (void)request:(FBRequest *)request didLoad:(id)result {
}
Sorry I got frustrated and deleted the code out of that method.
and then in the delegate I have the Facebook property
#property (nonatomic, retain) Facebook* Facebook;
There is other code in the file but this seemed like the most important code.
In whatever class you need to access the Facebook API from you need to include the following files (in the header file):
#import "FBConnect.h"
#import "YOUR_APP_DELEGATE.h"
and also make your class implement the FBRequestDelegate Delegate.
Then in order to use it you simply do the following:
YOURAPPDELEGATE *appDelegate = (YOURAPPDELEGATE*)[[UIApplication sharedApplication] delegate];
With that you can now access the Facebook property you have in your delegate allowing you to access the API.
Now you need to add the method - (void)request:(FBRequest *)request didLoad:(id)result in your view controller so you can receive the response.
So now you can call
[appDelegate.facebook requestWithGraphPath:#"APICALLHERE" andDelegate:self];
and you will now receive the response in the request:didLoad: method which you can then do whatever you want with.

Load a .nib at first app launch

I want to show the "intro" nib i made only at first app launch.
I was using the following code in my viewDidLoad but it seems to do nothing (even in ViewWillAppear). I tried to clean, remove the app from simulator and device and build again but nothing happened.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"]){
IntroViewController *intro = [[IntroViewController alloc] initWithNibName:nil bundle:nil];
intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
[self presentModalViewController:intro animated:YES];
[intro release];
[defaults setObject:[NSDate date] forKey:#"firstRun"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
I also tried to show a little UIAlertView at first launch, and it works! Am i failing to load the nib?
EDIT
I forgot to say it's a tab Bar based app and i've some code in my app delegate to highlight rows at the first three sessions of the app.
Any help appreciated!
Ok, add the following code and it should work:
- (void)Loadview_afterdelay{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"]){
IntroViewController *intro = [[IntroViewController alloc] initWithNibName:nil bundle:nil];
intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
[self presentModalViewController:intro animated:YES];
[intro release];
[defaults setObject:[NSDate date] forKey:#"firstRun"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)viewDidLoad {
[self performSelector:#selector(Loadview_afterdelay) withObject:nil afterDelay:0.5];
}
Apparently, in this case, you can't load a second view right after your view is loaded (viewdidload)
Therefor you just set a small delay before loading your second view.
Try the following:
IntroViewController *intro = [[IntroViewController alloc] initWithNibName:#"IntroViewController" bundle:nil];
intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
[self presentModalViewController:intro animated:YES];
[intro release];
I have added your .nib name to initWithNibName
Also, if you're using iOS 5 (and ARC), you should use this:
IntroViewController *intro = [[IntroViewController alloc] initWithNibName:#"IntroViewController" bundle:nil];
intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
[self presentModalViewController:intro animated:YES completion:nil];
Have you tried moving it to viewDidAppear?
It looks to me that you should place that code in your AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Modify the code above to what you need.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[application setStatusBarHidden:YES withAnimation:NO];
return YES;
}
It is there that your telling your application what view to start with.
So first time have a firstTimeViewController.
If you are doing it here don't animate the transition in.
Just animate the transition off.
Put a break point and check if you are going into the viewDidLoad method and if so check if your test is appropriate.
I suggest maybe using a BOOL in an NSNumber to check agains.
On top of that you may consider the option to put that in viewDidAppear.
As #Louis already answered the question very well already the only thing is It's just not compatible with the ARC since there are a [ release] What I'll write now isn't new but I'll declare it more for the beginner ones.
First you have to import the TourGuideViewController.h in your RootViewController.h which will be like similarly like this:
#import <UIKit/UIKit.h>
#import "TourGuideViewController.h"
#interface RootViewController : UITabBarController
{
TourGuideViewController *TourGuideViewController;
}
#property (nonatomic, strong) TourGuideViewController *TourGuideViewController;
#end
Then You have to synthesize and implement your function in your RootViewController.m file:
#import "RootViewController.h"
#interface RootViewController ()
#end
#implementation RootViewController
#synthesize TourGuideViewController = _TourGuideViewController;
- (void)viewDidLoad
{
[self performSelector:#selector(LoadTourGuide) withObject:nil afterDelay:0.5];
}
- (void)LoadTourGuide{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"FirstRun"]){
TourGuideViewController *TourGuideVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TourGuideViewController"];
TourGuideVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:TourGuideVC animated:YES completion:NULL];
[defaults setObject:[NSDate date] forKey:#"FirstRun"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
#end

Facebook iOS Login - Delegate methods not being called

I'm trying to implement the Facebook login for one of my applications and the delegate methods - fbDidLogin: / application: handleOpenURL: / fbDidNotLogin: aren't being called. Here is the code that I'm using. Any help is much appreciated!
- (void)viewDidLoad
{
...
facebook = [[Facebook alloc] initWithAppId:kFacebookAppId];
}
-(IBAction) loginWithFacebook:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray* permissions = [[NSArray arrayWithObjects:
#"email", nil] retain];
[facebook authorize:permissions delegate:self];
}
}
#pragma mark - Facebook delegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
-(void) fbDidNotLogin:(BOOL)cancelled {
}
-(void) fbDidLogout {
}
I also have the Facebook App ID in the info.plist file under the appropriate entry. I have break points in the loginWithFacebook: method and they hit successfully. the [facebook authorize:] method gets called as well.
Thanks,
Teja.
Oops, I've just realized the application: handleOpenURL: method is an iOS application delegate method. I had it in one of my UIViewControllers. Moved it to the right place and it works.
application: handleOpenURL: calls than also it doesn't calling delegate methods. what should be problem?