I am a newbie in iPhone app development.
I want to develop a iPhone app as when the app is launched there are two buttons displayed for the user.
Button 1 is for User Login .
Button 2 is for User Registration .
How to add view to each of the button, where if Login button is pressed then that view is loaded with few textfields and a button to login in whereas if Registration button is pressed then the registration view is loaded with few textfields and a button to confirm registration.
There are few tutorilas of multiple views but they have only one button on one view and pressing that button the next view is loaded with one button to load the next view and so on and so forth. In my case I want many buttons (atleast 2 at the moment) on one view while app is loaded and then depending upon the button pressed that view is loaded.
Any sample code or tutorial link will be much appreciated.
Thanks in advance.
make one method and registered button event to this method
[button1 addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
for e.g:
-(IBAction)buttonClicked : (id)sender
{
UIButton * btn = (UIButton *)sender;
if (btn == button1) {
LoginViewController * controller = [[LoginViewController alloc] initWithNibName:# "LoginViewController" bundle:nil];
[self.navigationController pushViewController : controller animated : YES];
[controller release];
} else if (btn == button2) {
RegisterViewController * controller = [[RegisterViewController alloc] initWithNibName:# "RegisterViewController" bundle:nil];
[self.navigationController pushViewController : controller animated : YES];
[controller release];
}
}
you want to make the views in interface builder, then on button one, you will use code like
ViewControllerSubClass1 *viewController1=[[ViewControllerSubClass1 alloc] initWithNibName:#"nibname1" bundle:nil];
[self.navigationController pushViewController:viewController1];
[viewController1 release];
for button two you would use
ViewControllerSubClass2 *viewController2=[[ViewControllerSubClass2 alloc] initWithNibName:#"nibname2" bundle:nil];
[self.navigationController pushViewController:viewController2];
[viewController2 release];
Related
I'm using this code in all ViewControllers to create back button:
self.btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[self.btnBack setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[self.btnBack addTarget:self action:#selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
-(void)cancel:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
But when I go to controller with UIWebView I need top tap 2 times to go back in ios 6.
And in ios 7 when I tap 1 time UIWebView disappeared show black screen with my navigation and on 2nd tap app crashes.
In all screen this works great maybe something special with UIWebView, I dont know.
Help please!
I push webview:
-(IBAction)doPrivacy:(id)sender
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
WebPageViewController *web = (WebPageViewController*)[storyBoard instantiateViewControllerWithIdentifier:#"WebPage"];
web.hidesBottomBarWhenPushed = YES;
web.urlToOpen = #"http://dfdfdf.co";
[self.navigationController pushViewController:web animated:YES];
}
I'm calling the same segue twice, you can fix this by unlinking the connection from the CELL DIRECTLY, to your segue, and having the segue connection originate at the top of the table hierarchy in IB, rather than nested inside the cell. Connect the segue from you View Controller itself, to the segue. If you have done this correct, when you select the segue, it should highlight the ENTIRE view it is coming from, not just the cell.
I am creating a button and added it to my toolBar like this:
UIButton *sendButtzon = [UIButton buttonWithType:UIButtonTypeRoundedRect];
sendButtzon.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[sendButtzon setTitle:#"More" forState:UIControlStateNormal];
sendButtzon.frame = CGRectMake(toolBar.bounds.size.width - 18.0f,
6.0f,
58.0f,
29.0f);
[toolBar addSubview:sendButtzon];
How can I open a new viewController (which i have a segue for named "MoreView")?
You implement the following action method:
-(void)buttonPressed:(UIButton*)sender
{
[self performSegueWithIdentifier:#"MoreView" sender:sender];
}
And link this to your button like so (add this line to the code in your question):
[sendButtzon addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
This causes a tap on the button to call the buttobPressed: method, which in turn performs the segue which you have defined in the storyboard.
For this you have to define an segue with name "MoreView" in story board.
or else you have to create UIViewControler on button click like this..
-(void)buttonPressed:(UIButton*)sender
{
UIViewController *destinationController = [[UIViewController alloc] init];
[self presentModalViewContrller:destinationController animated:YES];
}
or Create View Controller form story board.
-(void)buttonPressed:(UIButton*)sender
{
UIViewController *destinationController = [self.storyboard instantiateViewContrllerWithIdentifier:#"DestinationViewController "];
[self presentModalViewContrller:destinationController animated:YES];
}
Every UIViewController story board has property.
I have a UINavigationController and I have to keep the the default back button "the back arrow style" I just want to ask if I can change the back button action without build new one and change its style
AFAIK you cannot change the action of the default back button itself but you can place a UIBarButtonItem as leftBarButtonItem there and assign your own action.
If there is a leftBarButtonItem defined then this is shown and not the default back button.
However, keep the GUI guidelines in mind when doing tricks like this.
No. If you want a custom back button, you have to create a custom UIBarButtonItem, then assign it to the appropriate property:
self.navigationItem.backBarButtonItem = myCustomBackItem;
The back button in UINavigationBar is generated automatically as soon as u Push a new UIView. In order for you to customize the Back button is to Create a new UIToolBar + a UIBarButtonItem with custom view.
Code below is the sample to use a custom UIBarButtonItem in UIToolBar.
// create button
UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
// create button item -- possible because UIButton subclasses UIView!
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
// add to toolbar, or to a navbar (you should only have one of these!)
[toolbar setItems:[NSArray arrayWithObject:backItem]];
navItem.leftBarButtonItem = backItem;
Link below is the design of iOS buttons in PSD format for further modifications.
http://www.chrisandtennille.com/pictures/backbutton.psd
You can make custom button and can make action on it but you can not change default backButton action.....
self.navigationItem.leftBarButtonItem = getBackBtn;
The UINavigationController sends a message to it's delegate when it pushes and pops a ViewController.
You can find out when the back button gets pressed by implementing the following and adding <UINavigationControllerDelegate> in your .h file
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.delegate = self;
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
self.navigationController.delegate = nil;
}
-(void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated{
//Test here if the View Controller being shown next is right below the current
// ViewController in the navigation stack
//
//Test by:
// 1. comparing classes, or
// 2. checking for a unique tag that you previously assigned, or
// 3. comparing against the [navigationController viewControllers][n-2]
// where n is the number of items in the array
if ([viewController isKindOfClass:NSClassFromString(#"ViewControllerClassThatGetsPushedOnBACK")){
//back button has been pressed
}
if (viewController.tag == myUniqueTagIdentifier){
//back button has been pressed
}
if ([navigationController.viewControllers[navigationController.viewControllers.count-2]==viewController]){
//back button has been pressed
}
}
Apple Docs UINavigationController Class Reference:
The root view controller is at index 0 in the array, the back view
controller is at index n-2, and the top controller is at index n-1,
where n is the number of items in the array.
I need to implement the back button; (as in webpages, when you click the back button the screen will set focus to the previous screen).
I am not using a navigation control here, instead i want to do this using a Navigation bar and then adding a navigation button on it. Now when the user clicks on the navigation button the previous view should be displayed.
How should i do this.
My screen looks like this :
When i click on Hello, it should go to the previous screen.
Check out UINavigationController Class Regerence then try something like this:
- (void)pickerCancel {
[self dismissModalViewControllerAnimated:YES];
}
- (void)doSomething {
[myView.navigationItem setBackBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerCancel)] autorelease]];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:myView];
[navigation setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[navigation setDelegate:self];
[self presentModalViewController:navigation animated:YES];
[myView release];
[navigation release];
}
You really should use a UINavigationController as that is its entire purpose.
I currently have a window with a view attached that has three UIButtons. When the user presses a button, I would like the view to change to a new view using a NavigationController.
So right now I have the about button coded like so:
UIButton *aboutButton = [UIButton buttonWithType:UIButtonTypeCustom];
aboutButton.frame = CGRectMake(236, 240, 60, 60);
[aboutButton setImage:[UIImage imageNamed:#"About.png"]
forState:UIControlStateNormal];
[aboutButton addTarget:self action:#selector (aboutButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aboutButton];
And for the action function:
-(void)aboutButtonPressed {
UINavigationController *aboutView =[[UINavigationControlleralloc] initWithNibName:#"About" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[self.view addSubview:aboutView.view];
[aboutView release];
}
The About.xib file currently has nothing in it.
Now on to the problem... When the button is pressed a top navigation bar appears at the top but not in the animated sense like I have it coded. It just pops up. Also there is no back button to return to the view with the buttons on them. What am I doing wrong? I can provide more code or details if this is not clear enough.
Edit: Here is a picture to better understand what is happening.
After clicking the about button:
http://dl.dropbox.com/u/1481176/Screen%20shot%202010-12-09%20at%2011.28.40%20AM.png
Edit 2: Removed some wonky code for a back button that shouldn't of been there.
Most of the code after pushViewController looks dodgy.
-(void)aboutButtonPressed {
AboutViewController *aboutView =[[AboutViewController **alloc**] initWithNibName:#"About" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
That should at least push your empty view onto the stack and give you a back button automatically.