UINavigationBar title - three20

I need a custom leftBarButtonItem on my navigation bar because I need to perform a function in the view controller when the view is popped. I would like to have the back button have the correct title (of the view controller below). So two questions:
1) Can I get the title of the standard backBarButton item somehow, so that I can manually set my custom button's title?
or
2) Is there another hook/event that I can use that is only called when the user pops the view?
Re 2: I found one solution using:
NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
But this array is (null) for me.
PS I'm using Three20.

There's a UIViewController function which gets triggered when the view controller disappears from the screen:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
.....
}

Related

Could not set the title of tableview

I embedded tableviewcontroller into a navigation controller, set the title, but it does not show when I run the app.
What is wrong?
This is the setting of the tableviewcontroller
Here is the setting of the navigation controller
First check, your NavVC must be initial VC.
If not then try this piece of code
-(void) viewWillAppear:(BOOL)animated {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
you must be presenting the plain table view instead of navigation controller. What you need to do is make you tableview as root view for a navigation controller and present the navigation controller.
var nav: UINavigationController = UINavigationController(rootViewController: vc)
//then present the nav
Change the initial VC from Navigation Controller to your TableViewController
I did not set the StoryboardId for Navigation controller.

OS X - How can create only one window using segue and preserve data?

I have two View Controllers. First one have sendbutton that segue to second view controller. Action Segue is Show. Each time click the button, new window pops up again. I don't want create new window again, and I just want to only one second View Controller.
And my FirstViewController.m :
- (void)prepareForSegue:(NSStoryboardSegue*)segue sender:(id)sender{
SecondViewController *destinationScene = [segue destinationController];
destinationScene.receivedString = _nextOne.stringValue;
}
SecondViewController.m :
- (void)viewWillAppear{
if(_myMutableArray == nil) _myMutableArray = [NSMutableArray array];
[_myMutableArray addObject:_receivedString];
}
What I want to is send a string in the first View Controller's TextField repeatedly to next View Controller. And keep using only one second window that created at first time.
See my Storyboard
You can do this in IB without any code. Update the Presentation attribute for the view controller in the destination view from Multiple to Single:
https://stackoverflow.com/a/36104388/287963
Don't set up the segue to happen automatically. Create an IBAction for the button, test whether your second view controller is already on the screen, and either segue or not depending on whether it already exists.

I want to go to a specific tab in a UITabBarController from a view controller

First of all, let me say I'm an absolute beginner, so please forgive me if this is a stupid questions to be asking. And let me just add that I've spent hours/days trying to figure out how to solve this problem - including extensive searches on stackoverflow (maybe the answer is somewhere and I just don't now exactly what to search for :)...
But, let's continue: I have a small (?) problem with an Xcode storyboard project. Basically my project looks like this:
Navigation Controller -> View Controller 0 -> Tab Bar Controller -< View Controller 1, View Controller 2, View Controller 3.
When the user pushes 'button #2' in the View Controller 0, I'd like him/her to jump directly to 'View Controller 2'.
Would that be possible at all, and if so what code should use and excatly where should I put it.
Hope someone out there will help a newbie out :)
Regards,
Ulrik
Yes it is possible. You may show any view controller from any other.
You should simply add a segue from button #2 to View Controller 2. (I assume you have all your controllers in single storyboard)
Update: the above solution will show you View Controller 2 itself without tab bar controller.
Hard to tell in details without seeing the actual code. For more details you may refer to these documents:
View Controller Basics (especially part "Storyboards Help You Design Your User Interface")
Presenting View Controllers from Other View Controllers
Using View Controllers in Your App
Probably you'll come up with more concrete question.
Update
If you want to preselect desired view controller inside tabbar controller you may use the following code sketch. Here you can programmatically initiate a segue and do the desired pre-initialization inside prepareForSegue:sender: method.
static NSString * const kShowTabSegueID = #"ShowTab";
#interface ViewController ()
- (IBAction)buttonOnePressed;
- (IBAction)buttonTwoPressed;
- (IBAction)buttonThreePressed;
#end
#implementation ViewController
- (IBAction)buttonOnePressed
{
[self performSegueWithIdentifier:kShowTabSegueID
sender:#0];
}
- (IBAction)buttonTwoPressed
{
[self performSegueWithIdentifier:kShowTabSegueID
sender:#1];
}
- (IBAction)buttonThreePressed
{
[self performSegueWithIdentifier:kShowTabSegueID
sender:#2];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
if ([segue.identifier isEqual:kShowTabSegueID]) {
NSNumber *indexToShow = sender;
UITabBarController *tabBar = segue.destinationViewController;
[tabBar setSelectedIndex:indexToShow.unsignedIntegerValue];
}
}
#end
If you are simply trying to programatically switch tabs, its as simple as:
[self.tabBarController setSelectedIndex:1];
If I am understanding your flow correctly, from ViewController0 you would present ViewController1(that has a UITabBarController). In the viewWillAppear: set the selectedIndex for the tab controller (code above) to index 1, which would be ViewController2.
EDIT
After looking at your project, add this code to your BrainBreaksViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[self.tabBarController setSelectedIndex:1];
}
I added this, and it switches to the 2nd tab after pressing "Press this button to goto tab #1". Follow Max Ks answer if you would like to be able to have a button to open each specific tab.
So if I understand correctly you have a view controller outside the tabbar controller and you want to navigate to the second tab in the tabbar controller from that (outside)view controller, if that is the problem then, this is my solution and I hope it helps someone as I spent some time on this issue myself.
First connect the (outside)Viewcontroller 0 to the tabbar controller in the storyboard with modal segue and give it identifier - "showTabBar" (not as one of the tabs just a modal segue).
Then:
in Viewcontroller 0 declare:
var tabBarIndex: Int?
//function that will trigger the **MODAL** segue
private func loadTabBarController(atIndex: Int){
self.tabBarIndex = atIndex
self.performSegue(withIdentifier: "showTabBar", sender: self)
}
//in here you set the index of the destination tab and you are done
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showTabBar" {
let tabbarController = segue.destination as! UITabBarController
tabbarController.selectedIndex = self.tabBarIndex!
}
}
then you can navigate to ViewController 2 like that(don't forget it is 0 indexed):
self.loadTabBarController(atIndex: 1)
This is tested and working as of the day I am posting this answer using
Swift 3.0.2 / Xcode 8.2.1

Maintaining navigation controller while changing the views using a swipe gesture - iphone

In my app I have a list of items in a table view. Tapping on one of them, the app shows details of the item. Details appear and by back button of navigation controller, the app returns to list.
In the detail view I have implemented a method to swipe gesture in order to change the view to the details of the second element of the list and so on. It works.
The method that changes view is:
- (void)oneFingerSwipeLeft:(UITapGestureRecognizer *)recognizer {
int idx=currentIdx;
if (idx ==[todasLasTapas count]-1) { //last object vuelvo al primero
idx= -1;
}
ClassInfo *info =[allInfo objectAtIndex:idx+1];
//VIEW CONTROLLER
MoreInfo *moreInfoController =[[MoreInfo alloc]initWithNibName:#"MoreInfoController" bundle:nil];
//passing the details to view
moreInfoController.id = info.uniqueId;
moreInfoController.name = info.name;
// CHANGE TO NEXT VIEW
moreInfoController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:moreInfoController animated:YES];
}
The problem is that navigation controller disappears when the "second" view appears. My idea is that navigation remains in order to come back to the main table list.
Could someone help me please?
Unfortunately presenting modal view controllers happens outside of the UINavigationController stack. As a result, when you call presentModalViewController, you are bringing a controller onto the screen totally outside of stack of controllers managed by the UINavigationController.
You can preserve most of your implementation, but just switch out the presentation of the modal view controller, by simply adding MoreInfo's view as a subview to the current view.
If you'd like to preserve memory a little more gracefully, just switch between two UIView's (the currently showing view, and the next one to show - which is reused from the last detail view).
Here's an updated version of your code that could do this:
- (void)oneFingerSwipeLeft:(UITapGestureRecognizer *)recognizer {
int idx=currentIdx;
if (idx ==[todasLasTapas count]-1) { //last object vuelvo al primero
idx= -1;
}
ClassInfo *info =[allInfo objectAtIndex:idx+1];
//VIEW CONTROLLER
MoreInfo *moreInfoController =[[MoreInfo alloc]initWithNibName:#"MoreInfoController" bundle:nil];
//passing the details to view
moreInfoController.id = info.uniqueId;
moreInfoController.name = info.name;
[UIView transitionFromView:self.view
toView:moreInfoController.view
duration:2
options:UIViewAnimationOptionTransitionCrossDissolve
completion:^(BOOL finished) {
[self.view removeFromSuperview];
}];
}
Here's a post that shows how to implement a custom animation to dissolve to another UIView:
How to make dissolve animation on changing views on iphone?

How to use custom Navigationcontroller in AppDelegate by using a Storyboard

I've got a problem concerning Navigationcontroller in AppDelegate. I'm using a storyboard, which looks like this:
As a result of using Push notifications, i've got the following function in my AppDelegate File:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//...
}
When the notification arrives I want to initialize the "Detail View" - Controller which needs an ID as a parameter. This ID is part of my payload so it is present in didReceiveRemoteNotification.
I'd like to to the follwing:
DetailView *detail = [storyboard instantiateViewControllerWithIdentifier:#"detail"];
detail.conversationID = theID;
[self.navigationController pushViewController:detail animated:YES];
My question at this point is: how can I get the navigation controller? I've searched for a function like "getNavigationControllerByIdentifier" or something like this, but found nothing. I can't instantiate the Detail View Controller directly because the navigationbar is missing there.
I hope you understand what I mean - if you think my approach is completly wrong please correct me ;o)
Just another small information: It's not important for me that the back button in the Detail View Controller goes back to the Table View - it's enough when it links to the controller with the button "Load Table View".
Thank you for help!
UINavigationController is a UIViewController subclass and can also be assigned an identifier in the storyboard.
Use -instantiateViewControllerWithIdentifier: to create the UINavigationController and it's root view controller. You may need to instantiate all of the intermediate controllers in code and modify the navigation controller's viewControllers property to set up the appropriate navigation stack. This way when the app launches into the detail view, they can find their way back as if they had manually pushed all the way through via the interface.
You can use rootViewController on your window object.
UIViewController *rootViewController = self.window.rootViewController;
// You now have in rootViewController the view with your "Hello world" label and go button.
// Get the navigation controller of this view controller with:
UINavigationController *navigationController = rootViewController.navigationController;
// You can now use it to push your next view controller:
DetailViewController *detail = [navigationController.storyboard instantiateViewControllerWithIdentifier:#"detail"];
detail.conversationID = theID;
[navigationController pushViewController:detailViewController animated:YES];

Resources