removing a view controller on click of a button - ios

I am developing a library for a iOS app that will authenticate the users on the basis of the credential entered by them.
What I am doing is that, from a view controller say TestViewController I am setting a new view controller LoginScreenViewController on top of TestViewController when user taps a button by using the following code.
LoginScreenViewController * LoginScreen = [[LoginScreenViewController alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:LoginScreen.view];
This LoginScreenViewController contains a Cancel button, on which I want to remove the LoginScreenViewController. For this I am using following code
[self.view removeFromSuperview];
the problem is when I am executing this code, my app is crashing.
Since my LoginScreenViewController is defined by the Library, I want it to be independent of the caller's implementation.
EDIT:
In LoginScreenViewController.m I have used following code to add an event handler to the cancel button.
[button addTarget:self action:#selector(CancelButtonHandler:) forControlEvents:UIControlEventTouchUpInside];
The app is crashing when I am tapping the Cancel button. Even if the CancelButtonHandler does not contain any line of code.

You are trying to remove wrong view. To remove LoginScreen view do:
[LoginScreen.view removeFromSuperview];

Related

Switch between ViewControllers using a button?

I am very new to programming, especially iOS, so any answer should be given as if I am a baby with ADHD.
This is what I have in my buttonclick:
ItemCreateViewController *itemCreateViewConrtoller = [[ItemCreateViewController alloc] init];
[[self navigationController] pushViewController:itemCreateViewConrtoller animated:YES];
What am I missing?
There are a lot of reasons why this could be happening among which is your button is not triggering the intended action. Among the reason why a button could not trigger is your XIB button is not connected to the button object defined in your header and method file.
If you look at the .h file look at all your IBOutlet items there should be a circle on the left most column if it is darkened out it is connected other wise it is not, and you need to connect this in your XIB file.
If this is not the problem please display the code where you specify the selector it should be something like this:
[cancel_button addTarget:self action:#selector(Cancel:) forControlEvents:UIControlEventTouchUpInside];
Make sure that the selector is an IBAction function -
-(IBAction)Cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
return;
}
One other thing, any statement after pushViewController will get executed prior to any actual change in the view. The actual View change only happens once control is returned to IOS. This is significant if your push view controller command is within a long conditional process and you fail to code the return / exit properly.
Since you are using a XIB file for your second view, you need to create it as follows -
ItemCreateViewController *itemCreateViewConrtoller = [[ItemCreateViewController alloc] initWithNibName:#"ItemCreateViewController" bundle:nil]; //Ensure you use correct nib file name
Then you can push it
[[self navigationController] pushViewController:itemCreateViewConrtoller animated:YES];
But since you are using a Storyboard you could have just added the new view to your storyboard, linked the button to the new view with a "push" segue and everything would have been done for you.

A Global In-App Home Button

Building an interface, but I need a button, let's say its a round HOME button, that sits in the lower-right of the screen, that is always there, and will always return you back to the main screen.
It makes senses in the context of what I'm trying to accomplish.
Is there a clever/elegant way to do this, or am I restricted to just making a version of the Navigation Controller?
I think here you need to play somehow with navigation controller. In the bottom of application stack there is always RootViewController, over which all other controller pushed in the application stack with respect to application window.
So just follow these steps to fulfill your question requirments.
Assign RootViewController to your HomeViewController (On which you want to come again and again via a right bottom most of screen).
HomeViewController *homeViewController = self.window.rootViewController;
And Every time When click on that "Right most bottom button " call this.
[self popToRootViewController];
Hope this helps you !!
To do so make an interface for the home button
#interface stickyHomeButton :UIViewController
#end
#implementation stickyHomeButton
- (void)viewDidLoad
{
[super viewDidLoad];
BDSAppDelegate *mainDelegate=[[UIApplication sharedApplication] delegate];
UIButton *homeButton=[UIButton buttonWithType:UIButtonTypeCustom];
[homeButton setFrame:homeButtonRect];
//desing you home button as you want
[homeButton addTarget:self action:#selector(popToHomeViewController) forControlEvents:UIControlEventTouchUpInside];
[[mainDelegate window] addSubview:homeButton];
[[mainDelegate window] bringSubviewToFront:homeButton];
}
-(void)popToHomeViewController
{
[[self navigationController] popToRootViewControllerAnimated:YES];
}
#end
You are ready with you home button, now you need to do just a simple thing. all the viewController need to inherit stickyHomeButton
Or you could add a container view, full screen size, and present your modals inside that as childrenViewControllers, with the static button above this container view in the hierarchy...

Event button in view added not work, iOS

I developed app for iOS and i trie catch the event in a button, that button is in a xib and i create a method when touch up inside.
and i present de view the following form
DetailNoticiaController *detail = [[DetailNoticiaController alloc] init];
[self.view addSubview:detail.view];
detail.view contain the button, when the button is pressed, the app crashed and not show the error.
The problem is that detail deallocates as you don't save it somewhere. You may try code below:
DetailNoticiaController *detail = [[DetailNoticiaController alloc] init];
[self addChildViewController:detail];
[self.view addSubview:detail.view];
Helpful link - "Creating Custom Container View Controllers"

Most accepted way to load a new iOS view without leaving current storyboard

I am confused as to Storyboards in iOS. I want to use them and have my code be as modern as possible, but somewhat confused. Here is my problem:
In the main view of my app, you click a button and some time-consuming things happen behind the scenes (music plays, some files are concatenated). While this is happening, I want a menu with some text fields to slide up and let the user enter some info, and then he will click a button to dismiss this menu.
This menu can cover the full screen, but I don't want to segue to a new Storyboard entirely, because there is stuff going on in the background and we'll need to come back to the main view soon.
One thing I tried was to create a new storyboard for this menu and load it using instantiateViewControllerWithIdentifier, which works, but then I can't dismiss it later without the app crashing. But maybe there is a better way anyway? What is proper programming style on this?
Someone asked about my code, I think this is the only place I can put it? Here it is. In the main view:
AddInfoController *infoSheet = [[AddInfoController alloc] init]; //subclass of viewcontroller
infoSheet = [self.storyboard instantiateViewControllerWithIdentifier:#"AddInfoView"];
[self.view addSubview:infoSheet.view];
then in my AddInfoController class I have:
- (IBAction)clickedDoneButton:(id)sender {
[self removeFromParentViewController];
}
and in the AddInfoView storyboard, I have a button which is hooked to that IBAction. When it crashes, nothing appears in NSLog, it shows me some hex stuff in Thread 1 and a EXC_BAD_ACCESS error
Ok - if you are bent on using Storyboards and the app keeps crashing when you present the dismiss button, there's most certainly an error you are making. The console error message would be helpful. What are you seeing there.
If Storyboards is taking too much of my time, I would do it programmatically and simply present a Modal View. If you haven't tried before, try presenting a view controller modally. You can learn more about it here.
Create a UIView and UITextViews programmatically and them to the main view. Try something like this:
UIView *someView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 280, 420)];
UITextField * someTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 240, 44)]; // create uitextfield or something else as much as you want.
[someView addSubview:someTextField];
[someTextField release];
[self.view addSubview:someView];
[someView release];
And create a action button to dismiss the view.
It looks like you are calling removeFromParentViewController but you never actually add it as a child. I don't know if that is what is actually calling the crash, but basically the format to use is:
taken from Apple:
- (void) displayContentController: (UIViewController*) content
{
[self addChildViewController:content]; // 1
content.view.frame = [self frameForContentController]; // 2
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self]; // 3
}
Then after you are finished you call the equivalent methods in reverse:
- (void) hideContentController: (UIViewController*) content
{
[content willMoveToParentViewController:nil]; // 1
[content.view removeFromSuperview]; // 2
[content removeFromParentViewController]; // 3
}

Logout button navigationController Issue with the navigation bar

I have a login view in my application which loads the main menu view after the user enters the correct username and password. I'm using this to go to main menu view:
MenuViewController *menuViewController=[[MenuViewController alloc] initWithNibName:#"MenuViewController" bundle:nil];
menuViewController.title=#"Felton";
UINavigationController *tnavController=[[UINavigationController alloc] initWithRootViewController:menuViewController];
tnavController.view.frame=CGRectMake(0, 0, 1024, 748);
[menuViewController release];
self.navController=tnavController;
[navController release];
for(UIView *subview in self.view.subviews)
{
[subview removeFromSuperview];
}
[self.view addSubview:self.navController.view];
So my question is how exactly am I going to go back to the login page when the logout button is pressed. I've tried the same way as above but the problem I have is that each time the user presses the logout button the login page gets 1 navigation bar, and they pile on each other each time. Is there any way that I can stop this from happening? Because, my login page shouldn't have a navigation bar.
thanx in advance
Although it's not clear from your question what the exact flow through your app is, you certainly shouldn't be adding the navController view as a subview.
You'd be better placed to have the main menu as the rootViewController of your main window, and then present the login view controller on top of it (using presentModalViewController:).
Create a delegate protocol in your login view controller, and make the main menu the delegate. WHen they login, the delegate method fires, and the main menu dismisses the login view. When they log out, the main menu can present the login view again.

Resources