How To Change UIButton Of Same UIViewController? - iphone

I have a a UIViewController that is pushed to by two different views in my app.
One time it is a modal view, so I have the right navbar button set to Done and it dismisses the view.
At another time in my app, this same view is pushed to, but not modally, thus I don't want this button to show. I tried adding this when pushing it, but no luck.
self.navigationItem.rightBarButtonItem.enabled = NO;

You can check the parent view controller for whether it has the modalViewController property set
if (self.parentViewController.modalViewController == self)
{
// add button
}

Simple and effective -
self.navigationItem.rightBarButtonItem = nil;
Edit:
How can you add this when you are pushing this ? Add it in the viewWillAppear or viewDidLoad of the viewController you want to see this is in.
You can check for a certain condition.
If it is pushed from view 1, you can make it nil.
If it is shown modally from view 2, you can make it appear.
For this, you will have to make the viewControllers communicate with each other. For that, you will need to use NSUserDefaults and set an integer for a key.
You can assign two different integers logically and use them as the condition for showing/not showing the rightBarButtonItem.
Good Luck.

Related

Multiple instances of the same TabBarController after Modal Segue iOS

I have an app with one main TabBarController containing two tabs that control two different views, A & B. View A is a scrollView and View B is a TableView. When i initially load the app, the scrollview in view A is empty.
In order to add pages to my scrollView, I have set it up as follows: I go to view B and perform one modal segue to a view embedded with a navigationBar. The navigationBar only has one button, 'Cancel', which I use to dismiss the view. Otherwise, the user must click on an image an perform another modal segue to a different view. This view has no navigation bar, and has one button 'DONE', which I use to perform a modal segue back to the initial tabBarController.
Here's the problem: the page is added to the scrollView with no errors after I press 'DONE'. However, I believe I now have two instances of the same tabBarController floating around in memory. When I attempt to grab the views contained in the scrollView with a different button, it tells me that it is now empty (even though it was full during viewDidLoad and viewDidAppear).
How can I remove the initial tabBarController view or otherwise how can I segue back to the tabBarController that I have already allocated? Any help would be extremely appreciated! Thanks!
You shouldn't do a segue back to the original view controller. Rather, you should dismiss the current view controllers animated, and show your original tabBarController.
Inside the view you were segueing back from, add:
tabBarController *tabs = (tabBarController*)[[self presentingViewController]presentingViewController];
tabs.selectedViewController = [tabz.viewControllers objectAtIndex:0];
[[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
Then you will have the view A appear and still use the same allocation.

How to make transition from RootController to DetailController UITableview iphone sdk

I have a UITableView with some Cells.
I don't use NavigationController, so I'd like to use UIModalTransition to Switch from RootController, my TableViewcell, to my DetailView, but want to add a Navigation bar to attribute some actions, like Backbutton.
I don't want to use seguesTransition, I only used XIB File and any Storyboard.
I really don't know how to use the Modal Transition in TableView, anyone know how i can do it ?
Thanks.
have you tried
[self presentModalViewController:yourViewController animated:YES];
then you can set certain styles for transitions in it.
On the other view add a navigationbar and place a backbarbuttonitem and add an action to it to go back to your previous view.
Although why do you want to do it is unclear to me.
if you dont want a navigation controller on the first view, you can just set it to hidden.
self.navigationController.navigationBar.hidden = YES;
and then in the viewDidLoad of other view you can do the reverse i.e
self.navigationController.navigationBar.hidden = NO;
this is quite an easy way of achieving what you want with relatively less amount of code!!

stop view from disappearing

I am launching a viewController from another view controller via the push on its table views cell. Now on the second view controller I have a whole bunch of controls mainly test fields. I would like to by using the default back button provided in the second view controller so it'll be the title of the first view controller and keep in mind like I said default, so I don't want to create my own button for back on the second view controller. So would like to detect if the second view controller is exiting or disappearing or will disappear and based on certain conditions stop it from going back to the original caller view controller. I originally assumed it could be done in here:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
{
// So back button was pressed on the second view controller, so how do I stop it
// here from going back to the original view controller.
}
}
Or how do I do it? I can't seem to find a view controller return type BOOL method to call and stop it.
Thanks.
Note that method is called viewWillDisappear, not viewShouldDisappear, so Apple won't let you stop the view from disappearing at that point. And from the user's point of view, pressing the back button should indeed take them back. Instead you might consider hiding the back button under the circumstances where it's not allowed.
You can't prevent the view controller from closing however you can emulate it.
This line will replace the close button with a button that will call a homemade function
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(closeBtn)];
So you will be able to close the view if you want:
- (void) closeBtn
{
bool shouldClose=true;
// Your conditions here
if (shouldClose) [self dismissViewControllerAnimated:YES completion:nil];
}
sorry,i'm not good at english,so reading so long text is a little difficult for me.i can only get that you want to go back without a button. i known navigationController has a method to go back to previous view.
Make a custom back button and use self.navigationItem.hidesBackButton=YES
Add target to your custom back button and use it as you like. When you want to move the view you can use [self.navigationController popViewcontroller].

UIPopoverController on UITableViewCell

I have a UITableView cell with several UITextFields in it. When a user clicks into one of the textFields, a popover appears with some information. At first, they then had to click outside of either the textField or the popover to clear the popover before then clicking into the next textField. I therefore then added the cell's contentView to the popover controller's passThroughViews property so they can click through the different textFields in that cell at will without having to dismiss the popover controller first. However, it keeps the original popover open (which) is fine, and then opens another identical popover as well.
Is there a way I can tell if a popover is already open before sending the command to open another? I can't think of how to detect this?
If each cell controls the logic of the popover, you need to say to your custom cell to implement UIPopoverControllerDelegate protocol, set the delegate for the popover as self (the cell) and override popoverControllerDidDismissPopover like the following:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
[self.pop dismissPopoverAnimated:YES]; // hide the popover
self.pop = nil; // release the popover, this forces to create a fresh popover each time
}
If you want you can also remove the line self.pop = nil; but remember to release it in dealloc (I suppose you are not using ARC code since you are using retainCount). As bbum suggested you should't use retainCount to check objects existence.
In addition, each UIPopoverController instance has a property called popoverVisible if you want to see if a popover is already visible or not.
OK, I did it. For others who find this question. In addition to the other answers, this is how I did it.
I made my view controller a UIPopoverControllerDelegate.
I then created an BOOL called myPopoverControllerOpen.
When I created my popover I set the BOOL to yes. Using the delegate method popoverDidDismissPopover I then set the BOOL back to NO.
I then check on the state of this BOOL before presenting the popover.

UINavigationController TitleView not displayed from ViewController NavigationItem

Whenever I add a viewController to a navigationController while in landscape the title view appears on certain views but not on others. ie: I have a navigation controller, add 3 view controllers, first two show titleview appropriately, third one doesn't show one at all. But the navigation controller grabs the titleview from the ViewController like it's supposed to, I wrote the value of it to the console and it is correct, but it just doesn't show on the screen for whatever reason. Any ideas?
Oh yeah works perfectly while in portrait orientation.
Here's another fun part, if I push the trouble view controller into the navigationController in landscape the titleView isn't there, then without any user interaction, I rotate the device back to portrait and the titleView appears, then I rotate the device back to landscape and it stays!
It's like the drawing of my TitleView was blocked even though I used InvokeOnMainThread. Nothing is running in the main thread (or anywhere for that matter) during that call.
Here's my structure:
Window
TabBarController
NavigationController
ViewController
NavigationController
ViewController
Here's my order of operations:
Create View Controller
Add Title view to view controller
Push View Controller onto NavigationController (InvokeOnMainThread)
Have you tried setting the controller title after the controller is pushed? This kind of behavior happens to me and the way to make sure the title appears is to mandatory set the navBar title in the viewDidLoad or viewWillAppear method as follows:
self.navigationController.navigationBar.topItem.title = #"The title";
or
self.navigationItem.title = #"The Title";
Other thing that happened to me is to set the leftBarButton or RightBarButton of a navigation bar without success in the viewDidLoad method, but they appear correctly when setting the bar buttons in the viewWillAppear method.
Hope this helps.
I think your problem maybe that when your function is called, the navigation item is nil. So when you call self.navigationITem.title, it do nothing. Later, when the view is rotated, the navigationItem is not nil anymore so changing the title works.
If you do the code in ViewDidLoad function beware that ViewDidLoad is called the first time someone calls viewController.view and not the first time the view is displayed. So the view may not be in a navigationController yet.
For example, this can happend if you do :
viewController.view.backgroundColor = ... ;
[navigationController pushViewController:viewController]
The first line will call ViewDidLoad even if the controller is not in a navigationController yet.