How to load a UIView from a tap? - iphone

I'm new to iPhone Development and I did some examples and seen code and code and more code but I still can't get the
when the user taps here show this view using this animation, and go back after (user taps a back button)
I did some Tab Bar examples, Utility examples, etc but when I start a project from scratch the code never does what I want :-/
every time I create a View (xib) I also create the controller (h and m files), as all examples are like this, and I have no idea if I can only create 4 Views and just have one controller :-(
when a user taps a UITableCell how can I load a new view using an animation? and how can I go back to the UITableCell the user was?
kind'a (in C#)
myNewForm f = new myNewForm();
f.show();
...
this.Close();
If someone can share some knowledge or a tutorial or a screencast, I will greatly appreciate
Thank you!

Tap a cell and show a new view, if your table view has a UINavigationBar:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == myRow) {
MyViewController *vc = [[MyViewController alloc] init];
[self.navigationController pushViewController:uvc animated:YES];
[vc release];
}
}
Then the user can tap back as well. You can present modally as well, but would need to implement a way to return to the table view, by using:
[self presentModalViewController:vc animated:YES];
But the simplest base case is this, which you can see used in any template app's AppDelegate:
[window addSubview:vc.view];
Remember that the OS is doing a lot for you, including teaching you about MVC by encouraging the creating of controller classes.

You want to read about UINavigationController. Maybe also read the "View Programming Guide for iPhone OS", all available at http://developer.apple.com.

Related

Xcode sub navigation views

Does anyone know of any good tutorials for the following please?
Im new to Xcode and dont know where to start with this.
I have a ViewController that is the root View and has 6 navigation buttons (UIButton) on it. Depending on which button that is clicked, the user will see a sub-navigation View of that section with further button options on it.
So e.g top level will have buttons Where to Eat, What to Do...
Then clicking on Where to Eat will show Restaurants, Fast Food ....etc
I would like to do this programatically. I can do it using Storyboards and using multiple views, but it gets very messy as there are a lot of views on the screen eventually.
I have followed a tutorial HERE on how it is done for TableViewControllers, but I need something similar for buttons.
Im not sure what this function is called - have been searching for sub-navigation for the last while but nothing matches what I need to accomplish this.
Check out UIViewController's method presentViewController:animated:completion: method. It is available in iOS 5.0 and up. Let's say you have one of the button's linked to run the buttonOneActivated: method:
-(IBAction)buttonOneActivated:(id)sender
{
UISubViewController *subViewController = [[UISubViewController alloc] init];
[self presentViewController:subViewController
animated:YES
completion:NULL];
}
And in UISubViewController's implementation, let's say you have another button in order to return to the parent:
-(IBAction)returnToParent:(id)sender
{
[[self presentingViewController] dismissViewControllerAnimated:YES
completion:NULL];
}

Attached a new controller to existing storyboard

I'm doing some iPhone development, and I'm using Storyboards to mock up and expedite my development.
I didn't came from the conventional way of doing things, do I have to?
Anyway, I have storyboard,
TableViewController
NavigationController->ViewController->TabViewController [
AnotherViewController
I wanted to add a new ViewController attached to the TableViewController so that when I click on the row item it will show it on the other view, however;
I can't find a way how to connect the new ViewController into the TableViewController (vice versa)
So I tried the conventional way of doing things on the
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I put the ff:
CViewController *controller = [[CViewController alloc] initWithNibName:????? bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
I tried to give the Controller an identifier on the Attributes Inspector but does not work and is giving me the following crash stack:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/paulvincentong/Library/Application Support/iPhone Simulator/5.1/Applications/A1C369F8-9EAD-4794-8861-945C73F7FE0B/SyncProto.app> (loaded)' with name 'ControllerViewName'
If I remove the Identifier, I'll get a no NibName exception;
What should I do? I know it should just be the same as I was able to go as far as four levels of relating controllers, there might be something at the back of my head that is confusing me...
TIA,
try this in your cellForRow
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard"
bundle:nil];
ViewCont * view = (ViewCont *) [storyboard
instantiateViewControllerWithIdentifier:#"view"];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self view animated:YES completion:nil];
}
Make sure you have set your storyboard also in the project manager.
Are u using IB? There you can drag directly.
Otherwise in storyboards you don't use the push method you stated. You use performSegueWithIdentifier. Storyboards are about segues. You can segue directly or use target actions.
Again the strength of storyboards is in their visual representation. Use IB.
Otherwise you might also consider delegation (ESP for ipad).
After playing around... I found the suitable answer to my question;
My problem appears to be cause of
Confusion - a new programmer's dilemma.. tho the question is a common one, we can never really deviate if its really the same with those already existing and asked before only up until we tried it ourselves and found out that it really was.
The solution could vary from case to case; and for me I tried to do the ff:
Approach 1: (using the "instantiateViewControllerWithIdentifier") with a segue arrow
Create the view to be added
Position your mouse pointer to the Nib element source view controller you want to trigger the segue then hold control
Drag you mouse to the destination view controller
A popup will appear, choose your segue type (push, modal, custom) then automatically xcode will create the necessary connections
Select your destination view controller and on the Attribute Inspector there is a Collapsible group with Identifier field -> on this field you will put your custom identifier for the view
So the source code would look like;
ViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:#"CustomViewName"];
[self.navigationController pushViewController:view animated:YES];
Approach 2: (using the "instantiateViewControllerWithIdentifier") without the segue arrow
Create the view to be added
Provide the new controller with the necessary identifier then you're good to go
Using the same source as defined above
However, using this approach will break your storyboard's consistency and why use this? (still something I have to find out), its not obvious when you start looking for the segue arrow that was never there, albeit it works.
This code will not work on the basis that I don't have a nib with a name ????,
CViewController *controller = [[CViewController alloc] initWithNibName:????? bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
and since its on a storyboard and assuming everything was done using the storyboard, then time to forget about the nibName because it will never happen..
Hope that this idea would help a few...
Learning is a thorough process yet its a very interesting thing to do...
Thanks.

Navigate from one view to another using UIButton

I have an application with 2 views . In the first one I have a button which when I clicked the user should go to the second view. I tried what is explained before here from Karoley , but it does not work . When I click the button nothing happened?
Here is the code of my action :
-(IBAction)gotoSecondPage:(id) sender{
NSLog(#"In gotoSecondPage");
LeoActionViewController *aSecondPageController =
[[LeoActionViewController alloc]
initWithNibName:#"LeoActionViewController"
bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:aSecondPageController animated:YES];
}
LeoActionViewCOntroller is a controler for a second view.
It just do not switch to a second view. I do not know why
I put code your problem this will help you. First of all, you declare method and open .xib file and then connect to that button with selected touchupinside connection.
In the .h file:
- (IBAction)gotoSecondPage:(id) sender;
In the .m file:
- (IBAction)gotoSecondPage:(id) sender
{
NSLog(#"In gotoSecondPage");
LeoActionViewController *aSecondPageController =
[[LeoActionViewController alloc]
initWithNibName:#"LeoActionViewController"
bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];
[aSecondPageController release];
}
I'm not sure in what capacity you want to switch views.
What immediately comes to mind is that you want a Navigation Controller. This is an object that lets you put view controllers on a stack and push and pop them to show and hide them. It creates a navigation pathway through your app and is easy to use. It also facilitates the 'standard' navigation bar which is found in many iphone apps.
If you just want to change one view for another view you can do many things including hiding and showing different views using setHidden:(bool)hidden. Otherwise you can use addSubview:(UIView *)view and removeFromSuperview to add and remove views completely from the superview.

Help Menu iPhone

I have a design question/technical question about my iPhone app.
I have a pretty simple (read really really simple) single view application. And it does everything that I need it to do. However I find myself in need of a help view. And I really don't quite know what to do!
I have a simple helpButton() method in my main view controller, and I really just want to display a scrollview with a bunch of images that show what to do during the use of my app. However, should I make a new viewcontroller class? How do I call it from my method?
Really I was thinking of an unfortunately simple method, just putting a scrollview behind everything and hiding it. Then showing it when the IBAction is called. Horrible...
Sorry if this is elementary, I haven't needed to do anything more yet!
You can push a modalViewController. To do that just make a new viewController with the scrollview and associated data in it, then
MyViewController *myViewController = [[MyViewController alloc] init];
[self presentModalViewController:myViewController animated:YES];
Create an IBAction in your new viewController and a hooked up button to that action to dismiss the modalView (something like this:
IBAction done {
[self dismissModalViewControllerAnimated:YES];
}
A couple options:
1) Create a new UIView object, either programmatically, or even in your existing XIB file. Use the [self.view addSubview:view] method to display it.
2) Create a new UIViewController with its own XIB file. Use [self presentModalViewController:anaimated:] to display it.
Either way, you'll need to add something to the new view to dismiss it when you're done.

How to create a Main Menu that links to another view? (When developing iPhone Apps)

I'm a newbie programmer and I have played around with the SDK for a while. How do you create a homepage that has a button that will bring you to another view (the content of the app)??? If you know can you list the steps in order or link a tutorial site (It woul be awesome if the steps had code with it I needed). Thanks!
I would recommend getting a good book on iPhone programming. What you want to do is set up a UINavigationController, add the button to the first view, set the action of the button's touchUpInside to call the showContent method. Then implement the showContent method to push the Content view controller.
- (IBAction) showContent{
ContentViewController *myContentController = [[ContentViewController alloc] init];
myContentController.title = #"Content";
[self.navigationController pushViewController:myContentController animated:YES];
[myContentController release];
}
Have a read up on the documentation for pushViewController and UINavigationControllers, then follow this tutorial.