How to keep an NSArray available when switching to other view controller - ios

Hi in VC1 I have an NSMutableArray displaying results. I want to keep that array alive even when users clicks to a different tab (so they don't have to search again) until the user searches again.
I have a strong pointer to it, but it seems to unload when I leave the view.
Not much code to show (_resultsArray is set from a previous controller using delegates, so it loads with the results already)
- (void)viewDidLoad
{
[super viewDidLoad];
_resultsTableView.dataSource=self;
_resultsTableView.delegate=self;
[self.navigationController.navigationBar setHidden:YES];
}
//then standard tableview delegate methods...
This code is to try to figure out how to segue tab bar to share info.
(in prepareforsegue)
Currently in Search VC. Now I have results I want to give to resultsIndexVC. The code below attempts this.
This is placed in current (search VC) prepare for segue.
ResultsIndexViewController* vc = [[ResultsIndexViewController alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (ResultsIndexViewController *)[[tbc customizableViewControllers] objectAtIndex:1];
vc.SearchDelegate=self;//crash here (normally works if using regular segue)
vc.resultsArray = _temporaryResultsArray;
vc.originalQuery=_queryArray;
Thanks

Issue is that I was pushing a VC. Instead I used the tabbar (which doesnt release the object when switching tabs)

Related

Dismiss a previous Viewcontroller

I am current developing an game with multiple viewcontroller.
VC1(intro) -> VC2(game selection) ->VC3,VC4,VC5(games) -> VC6(display marks)
all the arrows are modal segue
I use unwind segue to go back from VC6 to VC2 and would like to replay. However, when select a game to go to VC3, the VC3 is not brand new as expected. It appears as the finished game condition.
If I really want to replay it after showing the marks, how can I achieve this with segue and Viewcontroller?
Moreover, how can I completely remove the instance of a previous viewcontroller?
You can go back on the parentViewController chain:
UIViewController *viewController = nil;
do {
viewController = self.parentViewController;
} while (![self isViewControllerImLookingFor:viewController]);
Or you could implement a custom navigation stack manager, and store an array of navigation controllers, similar to how UINavigationController does.
You can manipulate the view controller array of your navigation controller like so
NSMutableArray *navigarray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[navigarray removeLastObject]; //navigarray contains all vcs
[[self navigationController] setViewControllers:navigarray animated:YES];

Present Modal View Controller from inside Popover View

So in my universal app I have a section where a person can look at an existing list of notes from our system (retrieved through a simple web service) and then also create a new note if they want. So for the iphone it's pretty simple layout, a TableViewController for displaying the list with a "Add" button on the NavigationBar that presents the modalview for adding the new item. On the iPad though, the same layout has a lot of wasted space so I opted to go with the popOver method to show the list in a popOver and then let them add from there. My problem is that when the user clicks on the Add button within the PopOver view, the modal view comes up full screen instead of just coming up within the popover view. Here's the code I have so far:
-(void) AddButtonPressed:(id)sender {
NewNoteVC *newNote = [[[NewNoteVC alloc] initWithNibName:#"NewNoteVC" bundle:nil] autorelease];
newNote.defaultClientID = defaultClientID;
UINavigationController *navCon = [[[UINavigationController alloc] initWithRootViewController:newNote] autorelease];
if ([isPopOver isEqualToString:#"YES"]) {
[navCon setModalInPopover:YES];
[self.navigationController setModalInPopover:YES];
[self.navigationController presentModalViewController:navCon animated:YES];
}
else {
[self.navigationController presentModalViewController:navCon animated:YES];
}
}
The "isPopOver" string is just a placeholder sent from the previous screen that called this TableView (I know I can switch this to a boolean for better performance I just put this together real quick to try it out). I know I messed up somewhere, I just don't know what setting I need to get this working correctly.
You need to define the view controller's modalPresentationStyle to be "current context".
navCon.modalPresentationStyle = UIModalPresentationCurrentContext;
This will result in modal view controller filling the popover like the popover's root controller.
Try using the presentViewController:animated:completion: instead of presentModalViewController:animated: and set self.navigationController.definesPresentationContext = YES

TTNavigator not pushing onto navigation stack

I am using the TTTableView class from the Three20 framework to create table view cells with styled content, including HTML with URLs. The cells look and work almost fine. The URL are picked up, and tapping on one of the fire the appropriate delegate method. However, the URL is open in a TTWebController the TTWebController does not have a back arrow to pop the view of the navigation stack.
Heres my code:
TTTableStyledTextItem *messageItem = [TTTableStyledTextItem itemWithText:[TTStyledText textFromXHTML:message lineBreaks:YES URLs:YES]];
messageItem.delegate = self;
TTListDataSource *dataSource = [[TTListDataSource alloc] init];
[dataSource.items addObject:messageItem];
TTNavigator* navigator = [TTNavigator navigator];
navigator.delegate = self;
navigator.window = [UIApplication sharedApplication].delegate.window;
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]];
self.tableView.dataSource = dataSource;
self.tableView.delegate = self;
The URLs are highlighted in the cells and tapping one fires this method:
- (BOOL)navigator: (TTBaseNavigator *)navigator shouldOpenURL:(NSURL *) URL {
return YES;
}
The TTWebController does not seem to be pushed onto the navigation stack, it is just "shown" without a back arrow. Any thoughts?
Update with my solution
After playing around some more I think the problem is is that I am trying to use the Three20 URL navigation method to push a new view controller while at the same time using a regular iOS UINavigationController. The point at which the TTWebcontroller is being shown is the first view controller on the Three20 navigation stack, and as such is the root view controller and so does not have any notion of "going back" to a previous view. 
Here is my work around:
- (BOOL)navigator: (TTBaseNavigator *)navigator shouldOpenURL:(NSURL *) URL {
// setup a webcontroller with the URL and push it
TTWebController *webController = [[TTWebController alloc] init];
[webController openURL:URL];
[self.navigationController pushViewController:webController animated:YES];
[webController release];
// dont navigate with TTBaseNavigator
// this does not use the regular UINavigation stack and
// ... the new view becomes rootview and has no back button
return NO;
}
Hope this helps some one.
That might happen depending on how you presented the viewController containing the tableView. It that viewController pushed onto a UINavigationController or invoked per url (in that case Three20 would automatically create a navigationController for you).
You need to make sure, that a navigationController onto with the WebController can be pushed exists.

Set UITabBarItem title programmatically?

My app is based on a tab bar architecture. In order to have an easy internationalisation mechanic, I do not write any string in XIB files. viewdidload permits to change programmaticaly the strings in the views.
When my app launches, I can see the 4 tabs. But in fact only the first one loads its view controller. The others wait for user click to be loaded.
Tabs title can be changed using [self setTitle:#"Mouhahaha"]; in viewDidLoad of loaded view controller.
If I want to keep my internationalisation mechanic available, I do not set in my XIB the name of tabbar items. But, as at start all tab' view controllers are not loaded, I have blank titles for some tabs. The right title is set only when the user click on the tab.
I am looking for a way to set this title programaticaly for each tabbaritem. Do you have hints ?
Thanks a lot.
kheraud
my preferred method of doing this programmatically together with the storyboard is to make a subclass of UITabBarController, have my tab bar controller scene in my storyboard use the new subclass (with 3 UIViewController relationships from the tab bar controller to the desired view controller in the case below), and then override viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSArray* titleKeys = [NSArray arrayWithObjects:#"top places",
#"localizablekey1",
#"localizablekey2",
#"localizablekey3",
nil];
[super viewWillAppear:animated];
int count = 0; for (UIViewController* viewController in self.viewControllers)
viewController.tabBarItem.title = NSLocalizedString([titleKeys objectAtIndex:count++], nil);
}
All you need to do is make an instance of UITabBarController, then alloc any views you want in it, then set the UITabBarController views. Make sure that your TabBarController is the one that is visible. Also make sure that any viewControllers you want in your tab bar are being imported with #import.
UITabBarController *c = [self tabBarController];
SecondViewController *s = [[SecondViewController alloc] init];
[s setTitle:#"Whatever"];
c.viewControllers = [NSArray arrayWithObjects:self, s, nil];
[s release];
Put this in the viewDidLoad method of the first controller allocated.
Alternatively you could just change the way your ApplicationDelegate sets up the TabBarController, but i won't go into that.
Hope this helps

Cannot pass messages between main view controller and popover view

I'm seem unable to get any kind of communication going between my Main View Controller and a Table View Controller which is being displayed inside a Popover View (iPad).
I'm setting up the Table View inside a Navigation Controller in the usual way:
// create popover
if (self.popoverController == nil) {
filesViewController = [[[MyTableViewController alloc] initWithFiles:fileList] autorelease];
UINavigationController *navCtrl = [[[UINavigationController alloc] initWithRootViewController:filesViewController] autorelease];
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:navCtrl];
self.popoverController.delegate = self;
// resize popover
self.popoverController.popoverContentSize = CGSizeMake(320.0, 44 + [fileList count] * 44);
}
Everything is working fine, and I'm passing an array of file names (fileList) into the Table View, which is held in the Table View as an array called listOfFiles. The Table View displays the filenames, and when one is selected by the user I want to pass that filename back to the Main View Controller. However, I cannot get any communication going back from the Table View's didSelectRowAtIndexPath method to the Main VC. I've tried all sorts of outlets going in various directions, and I've tried creating a new object in didSelectRowAtIndexPath to handle the filename coming from the Table View. I can pass the filename out to the new object, but when I try to send that into the Main VC it is null again. Everything I send to my Main VC while that popover is active comes up as null.
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
NSLog(#"%#", handler.addressForImageFile);
self.popoverController = nil;
[self.popoverController release];
}
Is there some reason why my Main VC won't get anything but null objects from my Table View? I've spent days trying so many different things. I feel like there's some fundamental gap in my knowledge of how popovers work. Surely there is a simple way to send a string back to my Main VC when it is selected from the Table View?
Thanks so much for any help!
There's propably a much better way to do this, but depending on the goal of passing the string, one way could be to use NSUserDefaults.

Resources