ViewdidAppear fires for the second VC before the first - ios

2015-02-06 23:23:35.976 a[47551:2124298] second view did load
2015-02-06 23:23:35.978 a[47551:2124298] first view did load
2015-02-06 23:23:36.060 a[47551:2124298] second view did appear
2015-02-06 23:23:36.062 a[47551:2124298] first view did appear
The above describes my problem. I want to do stuff only after the second view appears, but all the events first as soon as I launch the app.
The first view:
#interface AddViewController ()
#end
#implementation AddViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"first view did load");
// Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"first view did appear");
}
- (IBAction)addBtnPressed:(id)sender {
NSLog(#"add Btn pressed");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ViewController *destVC = [segue destinationViewController];
destVC.destStation = self.destField.text;
}
The second view:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"second view did load");
manager = [[ CLLocationManager alloc] init];
geocoder = [[ CLGeocoder alloc] init];
[manager requestWhenInUseAuthorization];
// [manager requestAlwaysAuthorization];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"second view did appear");
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
NSLog(#"setting destStation %#", self.destStation);
self.destField.text = self.destStation;
self.destStationLoc = [LocationCalc findDestStationLoc:self.destStation stationInfos:[StationDB database].stationInfos];
}
The segue is connected from the button from the first VC.
Am I doing something wrong?

Related

Screen doesn't update when I change a UILabel's text

My Environment is Xcode 6.3, target is iOS8.3 for iPad.
I'm learning the Master-Detail view by using Xcode's template. Some strange problem appears when I try to set a label's text in the detail view:
If I put the statement which changes the label's text in P1 position, it works.
But if I put the statement in P2 position, it doesn't work.
Under both circumstances, the detailDescriptionLabel.text was changed, but only the P1 statement actually update the screen.(I have checked that function configureView() was called both times)
Can anybody helps me? Thanks a lot.
Below are my code snippets:
MasterController.m:
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController ()
#property (copy, nonatomic) NSMutableArray *objects;
#end
#implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib];
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *path = [[NSBundle mainBundle] pathForResource:#"PresidentList" ofType:#"plist"];
NSDictionary *presidentInfo = [NSDictionary dictionaryWithContentsOfFile:path];
self.objects = [NSMutableArray arrayWithArray:[presidentInfo objectForKey:#"presidents"]];
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
//P1
//controller.detailItem = (self.objects[indexPath.row])[#"url"];
}
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = (self.objects[indexPath.row])[#"name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//P2
//self.detailViewController.detailItem = (self.objects[indexPath.row])[#"url"];
}
#end
DetailController.m:
#import "DetailViewController.h"
#interface DetailViewController ()
#property (strong, nonatomic) UIPopoverController *masterPopoverController;
#end
#implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem {
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView {
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = self.detailItem;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {
barButtonItem.title = NSLocalizedString(#"Master", #"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = pc;
}
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
#end
I guess you are using segues and that's the point of using the dedicated function (prepareForSegue) designated appositely to initialize values AFTER the view destination view has been anyway created.
Unless you instantiate and show the view by yourself in didSelectRowAtIndexPath there is no point in setting something to such a viewcontroller.
I'm going to guess that self.detailViewController is nil. Hence P2 will not work.
Difficult to say without seeing all the code but in prepareForSegue, you might want to assign it there.

How to restore subview of view controller using state restoration

I am using state restoration to restore previous views and data for my app.When a button is clicked in first view controller I am to pushing second view control.In this case I am able to restore second view controller data.But if I add second View Controller view as subview to first view controller when button is clicked in first view Controller, encodeRestorableStateWithCoder: and decodeRestorableStateWithCoder: were not called in second view controller.Unable to restore second view controller data.Do I need to do any other configurations to restore subview data ?
//Firstviewcontroller
-(IBAction)moveToNextViewController:(id)sender {
SecondVeiwController *sec_VC=[[SecondVeiwController alloc]initWithNibName:#"SecondVeiwController" bundle:[NSBundle mainBundle ]];
sec_VC.restorationIdentifier=#"SecondVeiwController";
[self.view addSubview:tab_cnt_1.view];
}
//Secondviewcontroller
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.restorationIdentifier=#"Secondviewcontroller";
self.restorationClass=[self class];
}
return self;
}
+(UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
UIViewController * myViewController =
[[Secondviewcontroller alloc]
initWithNibName:#"Secondviewcontroller"
bundle:[NSBundle mainBundle]];
return myViewController;
}
It seems that iOS does not keep track of subviews, you have to encode the SecondViewController and then decode and add it as a child view controller with its view as a subview.
//FirstViewController
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.childViewControllers[0] forKey:#"childVC"];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder {
[super decodeRestorableStateWithCoder:coder];
UIViewController *v = [coder decodeObjectForKey:#"childVC"];
[self addChildViewController:v];
[self.view addSubview:v.view];
}
//SecondViewController
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.view.backgroundColor forKey:#"bgColor"];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder {
[super decodeRestorableStateWithCoder:coder];
UIColor *bgColor = [coder decodeObjectForKey:#"bgColor"];
[self.view setBackgroundColor:bgColor];
}

When trying to segue, nothing happens

I have an app I am working on, and I am trying to have the logo on for a bit longer, and fade out/slide out/effect when it's done.
Here's my setup:
The Tab Bar controller is not letting me place an Image View inside it, so I created a view to have it on.
I am trying to have the logo stay on for a bit, fade out, then automatically switch the view (Segue) to the Tab Bar controller.
This is what I get out of it: http://youtu.be/l4jL0BfpR2k
So here's my code:
//
// BDNLogoViewController.m
// Bronydom Network
//
// Created by name on 10/1/13.
// Copyright (c) 2013 name. All rights reserved.
//
#import "BDNLogoViewController.h"
#import "BDNTabBarController.h"
#import "BDNFirstViewController.h"
#interface BDNLogoViewController ()
#end
#implementation BDNLogoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[UIView animateWithDuration:1 animations:^{
_imageview.alpha = 0;
}];
//BDNTabBarController *viewController = [[BDNTabBarController alloc] init];
//[self.navigationController pushViewController:viewController animated:YES];
(void)#selector(seguePerform:);
}
- (void)seguePerform:(id)sender
{
//BDNTabBarController *myNewVC = [[BDNTabBarController alloc] init];
// do any setup you need for myNewVC
[self performSegueWithIdentifier:#"open" sender:sender];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Yes, "open" is defined as the segue id.
Do you guys have any ideas on how I could fix this?
To fix, add this
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self performSegueWithIdentifier:#"open" sender:self];
}
Remove this from your code
(void)#selector(seguePerform:);
// and all other unnecessary segue stuff you had

Media picker item selection

i want my code to allow me to pick multiple items via my ipod library. Currently I am able to select one song. i want to modify my code to allow for a series of songs to be added to the que. Currently selecting YES for "pick multiple items" allows for selection however playback consists of only one song. the first selected.
How may I change this. I have included a sample of my code below...
#implementation ProjectViewController
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[self performSelector:#selector(presentLibrary) withObject:nil afterDelay:0.1];
[super viewDidLoad];
}
-(void)presentLibrary
{
MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
// picker.navigationController.delegate = self;
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.prompt = NSLocalizedString (#"Select any song from the list", #"Prompt to user to choose some songs to play");
//[self.view addSubview:picker.view];
[self presentModalViewController: picker animated: YES];
//picker.view.frame = CGRectMake(picker.view.frame.origin.x, 0, picker.view.frame.size.width, picker.view.frame.size.height);
[picker release];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
- (void)dealloc {
[super dealloc];
}
- (IBAction)onlaunch:(id)sender
{
xxxxxxxx ViewController *viewController = [[xxxxxxxxxViewController alloc] init];
[self presentModalViewController:viewController animated:YES];
[viewController release];
}
-(void)viewDidAppear:(BOOL)animated
{
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
[self dismissModalViewControllerAnimated: NO];
//[mediaPicker.view removeFromSuperview];
//NSURL *url = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"screen1.mp4"]]; //test.caf
NSURL *url = [[mediaItemCollection.items objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];
NSLog(#"url:%#",url);
[[ShareInfo shareduserInfoManager] setSongUrl:url];
[self goToxxxxxxxxxyView];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
[self dismissModalViewControllerAnimated: NO];
[[ShareInfo shareduserInfoManager] setSongUrl:nil];
[self goToxxxxxxxxxView];
}
-(void)goToxxxxxxxView
{
xxxxxxxxxxxxxx *viewController = [[xxxxxxxxxxxxxxxr alloc] init];
[self presentModalViewController:viewController animated:YES];
[viewController release];
}
#end
Instead of
NSURL *url = [[mediaItemCollection.items objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];
Which is selecting the 1st song,
try
if (mediaItemCollection)
[musicPlayer setQueueWithItemCollection: mediaItemCollection];

Triggering the UIViewController connected to the UITabBarItem

Good day to you guys
I have an application that has a UITabBarController for tabbed-navigation... The view-controllers are mapped to their respective TabItems via a URL, just the same as that of Three20's TTNavigationSample App.
My problem is that inside a view controller of mine, i have a button that calls to another view controller which is also attached to a TabItem. When i trigger the button, the application throws an error. How can I resolve this?
In my TabBarController, i have this inside the viewDidLoad method:
-(void)viewDidLoad {
[self setTabURLs: [NSArrayWithObjects:
#"tt://bulletinBoard",
#"tt://contacts",
nil
]];
}
Sample .m file
#import "HabBarController.h"
#implementation TabBarController
- (void)viewDidLoad {
//these are variables like "tt/feed"
[self setTabURLs:[NSArray arrayWithObjects:
kAppFeedURLPath,
kAppHotURLPath,
kAppPostPhotoURLPath,
kAppGeneralActivityURLPath,
nil]];
}
- (UIViewController*)rootControllerForController:
(UIViewController*)controller {
if ([controller canContainControllers]) {
return controller;
} else {
UINavigationController* navController = [[[UINavigationController
alloc] init] autorelease];
[navController pushViewController:controller animated:NO];
return navController;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.tabBarController.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
#end

Resources