Create Backbutton programmatically without navigation controller - ios

In my iPhone App, I have 2 Storyboard, where I switch from Main.storyboard to Camera.storyboard and back. When I am on the Camera.storyboard I want to have the Backbutton functionality and look like the backbutton of the navigation conroller.
How do I do this best, without navigation controller? Is it possible to clone the navigationbar and backbutton somehow, which I then can connect with my back function?
Update
Had a missunderstanding in my thinking and now a solution with NavigationController. I embeded the CameraViewController in a NavigationController with IB in the Camera.storyboard and added the following code in the CameraViewController:
-(void)viewDidLoad {
}
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(back)];
self.navigationItem.rightBarButtonItem = cancelButtonItem;
}
I did use a cancel button instead a back button as, I did not find the back button and more important, the cancel was logical more correct in this situation.
Currently I switch to the Camera.storyboard with this code:
ViewController on Main.storyboard
#pragma mark - Segue to CameraView
-(void)toCameraView:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Camera" bundle:nil];
UIViewController *cameraViewController = [mainStoryboard instantiateInitialViewController];
cameraViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:cameraViewController animated:YES completion:nil];
}
and Back with:
CameraViewController on Camera.storyboard
- (IBAction)upload:(id)sender {
[[WebApi sharedInstance] uploadImage:self.imageView.image];
[self dismissViewControllerAnimated:NO completion:nil];
}

Related

presenting a view controller

I want to go to a view controller when the button "Cancel" is pressed. I tried using this method but when I click 'Cancel", it just displays a black screen. I am a beginner at IOS.
-(void) cancelButtonPressed {
homeView = (HomeViewController *)[[UIViewController alloc]init];
[self presentViewController:homeView animated:YES completion:nil];
}
EDITED FOR COMMENTS:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancelButtonPressed)];
self.navigationItem.leftBarButtonItem = cancelButton;
Try doing this:
HomeViewController *homeView = [[HomeViewController alloc]init];
[self presentViewController:homeView animated:YES completion:nil];
Also note that black is the default colour of the window. If you don't have any view controller named HomeViewController, and you present it, by default it will be visible as black colour to the user.
To Avoid this, you need to set the View for the newly presented view controller, which you can access by homeView.view
If you are using storyboard, then use storyboard ID:
HomeViewController *homeView = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self presentViewController:homeView animated:YES completion:nil];

How to pop back viewController from SWRevealViewController?

In my app after pressing the login button in my loginViewController, it push to front viewController of my SWRevealViewController. In my RearViewController I have the sign out button. When I press it it should pop to the back viewController again (LoginViewController). But even though I set the button click event like this it doesnt navigate to the loginViewController.
-(void)signOutClick :(id)sender
{
[self performSelectorInBackground:#selector(moveToLoginView) withObject:nil];
}
-(void)moveToLoginView
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
But when I swipe the view it gose to the back view.I want to disable that swipe feature to the back view. And I want to go to the back view when click on the sign out button in my RearViewController. How can I do that? Please help me.
Thanks
I am not sure but instead of using
[self.navigationController popToRootViewControllerAnimated:YES];
try using,
[self.navigationController popViewControllerAnimated:YES];
And to stop swipe gesture you can use code like,
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.revealViewController.panGestureRecognizer.enabled = NO;
}
You need to reset the top view controller if you want to perform these kind of actions off of a click. E.g.
get an instance of the ECSlidingViewController
ECSlidingViewController *slidingController = self.slidingViewController;
get the view controller you wish to be on the top
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"mainViewController"];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
set the topViewController property
slidingController.topViewController = nc;
[slidingController resetTopViewAnimated:NO];

Displaying modal views programmatically

I want to be able to show a viewController when a button is pressed.
I don't want to use a navigation controller anymore, is there a way to display it using a modal?
Here is how I am currently showing the viewController:
- (void) editButtonDidClicked: (UIButton *) button {
EditViewController *viewController = [EditViewController getInstanceWithTag:button.tag];
[self.navigationController pushViewController:viewController animated:YES];
}
You can try below code
I assume that you are using storyboard.
UIStoryboard *board = [UIStoryboard storyboardWithName:#"name" bundle:nil];
viewController *controller = [board instantiateViewControllerWithIdentifier:#"Identifier"]; // Identifier is define in storyboard
[self presentViewController:controller animated:YES completion:nil];
Please check out this link if you are still facing the problem.
Hope this helps you.

How to call a View Controller programmatically?

I have looked at all the tutorials I can find on this one, and I still don't have the answer. I need to call another view from the code. I am using UIStoryboards. I have changed the view many times by control-dragging from UIButtons, but now it must be from the code. I am trying to call the info page from the main menu if it is the first time the user has opened the app. I cannot seem to find a way to change the views from the code, however. All my views are controlled by the same files (ViewController2). The identifier of my main menu is ViewControllerMain, and the identifier of the info page is ViewControllerInfo. First I tried this:
[ViewControllerMain presentViewController: ViewControllerInfo
animated:YES
completion: NULL];
Then I tried making different UIViewControllers for each and saying:
[ViewController2 presentViewController: ViewController
animated:YES
completion: NULL];
Neither worked. For the first one, it says:
Use of undeclared identifier ViewControllerMain.
In the second one, it says:
unexpected interface name 'ViewController': expected identifier.
What can I do?
To create a view controller:
UIViewController * vc = [[UIViewController alloc] init];
To call a view controller (must be called from within another viewcontroller):
[self presentViewController:vc animated:YES completion:nil];
For one, use nil rather than null.
Loading a view controller from the storyboard:
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];
Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.
You need to instantiate the view controller from the storyboard and then show it:
ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerInfo"];
[self.navigationController pushViewController:infoController animated:YES];
This example assumes that you have a navigation controller in order to return to the previous view. You can of course also use presentViewController:animated:completion:. The main point is to have your storyboard instantiate your target view controller using the target view controller's ID.
Swift
This gets a view controller from the storyboard and presents it.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
Change the storyboard name, view controller name, and view controller id as appropriate.
You can call ViewController this way, If you want with NavigationController
1.In current Screen : Load new screen
VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
[self.navigationController pushViewController:addProjectViewController animated:YES];
2.1 In Loaded View : add below in .h file
#interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate>
2.2 In Loaded View : add below in .m file
#implementation VerifyExpViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.delegate = self;
[self setNavigationBar];
}
-(void)setNavigationBar
{
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"B_topbar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:#selector(onBackButtonTap:)];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Save.png"] style:UIBarButtonItemStylePlain target:self action:#selector(onSaveButtonTap:)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
}
-(void)onBackButtonTap:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)onSaveButtonTap:(id)sender
{
//todo for save button
}
#end
Hope this will be useful for someone there :)
There's 2 ways you can do this:
1, Create a segue to your ViewController in your Storyboard as explained in my answer here: How to perform a segue that is not related to user input in iOS 5?
2, Give your ViewController and identifier and call it using the code in my answer here: Call storyboard scene programmatically (without needing segue)?
main logic behind this is_,
NSString * storyboardIdentifier = #"SecondStoryBoard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];
UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:#"YourviewControllerIdentifer"];
[self presentViewController:UIVC animated:YES completion:nil];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone_iOS7" bundle:nil];
AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:#"accountView"];
// [self presentViewController:controller animated:YES completion:nil];
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topRootViewController.presentedViewController)
{
topRootViewController = topRootViewController.presentedViewController;
}
[topRootViewController presentViewController:controller animated:YES completion:nil];
Import the view controller class which you want to show and use the following code
KartViewController *viewKart = [[KartViewController alloc]initWithNibName:#"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];

TabBar back button is not showing in ios app

I have created a UINavigationBarButton where i have placed a button called "update".When update button is clicked it is navigating to SecondViewController.But the problem is in the SecondViewController TabBar is not showing.What I'm trying to do is when i tap the back button in the SecondViewController it should get back to RootViewController.Here is the code
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title=#"GooGle";
[self.navigationItem setLeftItemsSupplementBackButton:YES];
[self.navigationController setToolbarHidden:NO];
UIBarButtonItem *nextView = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonSystemItemPlay target:self action:#selector(updateAddress)];
self.navigationItem.rightBarButtonItem = nextView;
[nextView release];
}
-(void)updateAddress
{
SecondViewController *updateView=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController presentViewController:updateView animated:YES completion:Nil];
[self.navigationItem setLeftItemsSupplementBackButton:YES];
}
When you tap the back button in SecondViewController use
[self dismissViewControllerAnimated:YES completion:nil];
Since you are not using
[self.navigationController pushViewController:youViewController animated:YES];
the navigationbar won't be visible, if you do so you will have your navigationbar and a back button by its own and while pressing the back button it will itself move to the previous view
share your results

Resources