Click on button to change to table view in Objective-c - objective-c

I have two button in my program(figure A), I want to change to a table view(figure B) when I click on one button. Can someone please advise how to do that?
Thanks

Quite simple actually. In the button's handler method, create the table view controller and display it modally or push it on the navigation stack.
[myButton addTarget:self action:#selector(displayTableView:) forControlEvents:UIControlEventTouchUpInside];
-(void)displayTableView:(id)sender
{
MyTableViewController* myTVC = [[MyTableViewController alloc] initWithNibName:#"myTVC" bundle:nil];
[self.navigationController presentModalViewController:myTVC animated:YES];
//[self.navigationController pushViewController:myTVC animated:YES]; //2nd option
[myTVC release];
}

Related

modal segue to a xib from a xib?

I have an app that uses only xibs, no storyboards.
I have created a prompt xib that I would like to present modally (with the modal animation) from a table view controller xib (named TVC.xib) The TVC is nested in a navigation controller.
I can get the prompt to present itself, but I want it to present itself with a modal animation. Unfortunately, the presentModalViewController has been deprecated. What is the current option to present a view controller modally in code and have it animate the same way that modal presentations used to animate?
Here is my code: (in the TVC.m)
PromptViewController *promptVC = [[PromptViewController alloc] initWithNibName:#"PromptXib" bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:promptVC];
[self.navigationController presentViewController:navVC animated:YES completion:^{
NSLog(#"presented prompt vc");
}];
Ideally I could replace the method in the 3rd line with self.navigationController presentMODALViewController... etc, but it's deprecated.
You are looking for:
[self presentViewController:aViewController animated:animated completion:^{}];
But you should browse some tutorials to update the knowledge.
I've figured it out. I needed to set the transition style and presentation style on the view controller that I wanted to show. Here is my solution:
PromptViewController *promptVC = [[PromptViewController alloc] initWithNibName:#"PromptXib" bundle:nil];
promptVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
promptVC.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:promptVC animated:YES completion:nil];

iOS: dismiss a modal and push a new viewcontroller on the main NavigationController

Here is my problem (See illustration bellow):
I have a main screen (MainViewController) from which i present a form modal (ModalController) embedded in a Navigation controller.
After completing the form I want to present the result to the user and dismiss the modal.
The result is shown with a ItemViewController and it should be pushed on the main stack, i.e. in a navigation controller and if the user presses back, he returns to the main screen.
My problem is how to dismiss the modal and push the new view controller at the same time?
What I tried:
dismiss the modal and push the new view controller in the completion block using
self.parentViewController.navigationController pushViewController:itemViewController but only the modal is dismissed.
push the view controller the same way then dismiss, also without effect.
unwind the modal to the main screen and from the unwindSegue method, instantiate and push the new view controller. Unfortunately, the code below has the same effect..
- (IBAction)unwindSegue:(UIStoryboardSegue *)segue {
ItemViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ItemViewController"];
[self.navigationController pushViewController:viewController animated:YES];
}
Might be important
In the storyboard, ItemViewController is embedded in a NavigationController since it is defined as on the picture below:
Maybe this can help:
in viewDidLoad method of ItemViewController:
UIImage *backButtonImage = [UIImage imageNamed:#"back.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 55, 45);
[backButton addTarget:self
action:#selector(goHome)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
- (void)goHome{
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:0] animated:YES];
}

Apply back action to uiButton

I need to have a UIButton (with a class of RtnBtn) which basically replicates the back button action used in the navigation bar action at the end of a process - I'm fairly new to IOS dev and I'm not sure how to approach this - should it be done via a push or is there a better option to apply coded action directly to the button?
Can you post some code here, what you have done?, you can add custom back button by below code,
UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonBack setBackgroundImage:YOURImage forState:UIControlStateNormal];
[buttonBack addTarget:self action:#selector(backPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:buttonBack] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
Method to handle event,
-(void)backPressed
{
[self.navigationController popViewControllerAnimated:YES];
}
You need to create the button, specify a selector as it's action, and in that method call popViewController:animated: on your navigation controller.
If I did not understand wrong, you need this;
[self.navigationController popViewControllerAnimated:YES];

Pop controller after back bar button is pressed

I have a UINavigationController ans a chain of 3 simple controllers. Each one has a button. When press a button a next controller is Pushed. ViewController1 -> ViewController2 -> ViewController3. When I push a back button on the 3rd view i want to move to the first view. Using of backBarButtonItem is obligatory. Here is the code for second controller:
#import "ViewController2.h"
static BOOL isBackButtonPressed;
#implementation ViewController2
- (void)viewDidLoad {
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"back from 3" style:UIBarButtonItemStyleBordered target:nil action:nil];
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
if (isBackButtonPressed) {
[self.navigationController popViewControllerAnimated:YES];
} else {
isBackButtonPressed = YES;
}
[super viewWillAppear:animated];
}
#end
But when I press back button on the third view I return to the second view instead of the first view. Could you help me to return to the first view pressing back button on the third view.
I tried suggestions from answers but they don't help.
Adding a selector to backBarButtonItem doesn't help because it is never called.
Adding a [self.navigationController popToRootViewControllerAnimated:YES] in viewWillDisappear methos also doesn't work. I don't know why. I think that the actual problem is how backBarButtonItem works.
Any other suggestions?
The behaviour I try to achieve exists in the calendar on iPhone. When you rotate iPhone to landscape you get to the weeek view. Then go to the event details, and rotate to the portrait. When you press back button you will get to a day view not to a week view, so a controller with weekview is skipped.
After countless number of tries my solution was simply not use backBarButtonItem! As whatever i do it always goes to previous viewController instead of calling its selector
Instead I use only leftBarButtonItem for navigation, as it guarantees calling my action.
Here an example
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 22)];
[backButton setImage:[UIImage imageNamed:#"backbutton"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
This certainly calls backButtonPressed action.. This works both for IOS 6 and 7
No need to register a new selector for the back button, just do:
-(void)viewWillDisappear{
if ( [self.navigationController.viewControllers containsObject:self] )
//It means that the view controller was popped (back button pressed or whatever)
//so we'll just pop one more view controller
[self.navigationController popViewControllerAnimated:NO];
}
in your ViewController3 viewWillDisappear method
Try using this in your third view controller, this way you check if you have pressed the back button and directly pop to the root view controller which as you stated is your first view controller.
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
[super viewWillDisappear:animated];
}
I had the same problem as you beofre and fixed it like this:
You can capture the back button on the ViewController3 and before poping the view, remove ViewController2 from the navigation stack like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"back from 3" style:UIBarButtonItemStyleBordered target:self action:#selector(customBackPressed:)];
}
-(void)customBackPressed:(id)sender {
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: navigationController.viewControllers];
for (UIViewController *vc in viewControllers)
{
// If vc is ViewController2 type, remove it
}
navigationController.viewControllers = allViewControllers;
[self.navigationController popViewControllerAnimated:YES];
}
Because ViewController2 is not in the stack anymore, it will jump to ViewController1.
Also, if ViewController1 is the root view controller, you can just do:
[self.navigationController popToRootViewControllerAnimated:YES];

I have no Navigation Bar in a view called with an IBAction?

My main menu (a ViewController) is embedded in a NavigationController that I added in the storyboard in Xcode4.
I have a button in my menu, displaying a new view. To display it I use :
- (IBAction) display : (id) sender
{
if(!anotherView) anotherView = [[AnotherView alloc] initWithNibName:#"AnotherView" bundle:nil];
[self presentModalViewController:anotherView animated:NO];
}
My other view is correctly displayed with all its objects and elements. Excluding my NavigationController's bar, that doesn't appear. Why ?
Thanks for your advices
You are presenting the view modally what you probably meant was
[self.navigationController pushViewController:anotherView animated:YES]
of course what you really want to do is not mix and match storyboard and non storyboard flows unnecessarily like this and have the storyboard do this for you
you are presenting your viewController modally. If you want to use the navigation controller you have to push your view onto the navigation stack.
replace
[self presentModalViewController:anotherView animated:NO];
with
[self.navigationController pushViewController:anotherViewController animated:YES];
You can still present your view modally without losing the navigation bar. Try this code:
AnotherView *tempAnotherView = [[AnotherView alloc] initWithNibName:#"AnotherView" bundle:nil];
[self setAnotherView:tempAnotherView];
[tempAnotherView release];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:self.anotherView] autorelease];
[self.navigationController presentModalViewController:navController animated:YES];
Hope it helps! :)