to display one view in another view by clicking button - iphone

hi guys i am new to iphone apps development please help me to show another view from one view using buttons

Thats a very vague question... here is a start - you can look up some of the syntax and research a bit:
this in viewDidLoad:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Click" forState:UIControlStateNormal];
[button addTarget:self action:#selector(showView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button release];
then this method
- (void)showView {
UIView *view = [[UIView alloc] initWithFrame:CGRect(0,0,320,460)];
[self.view addSubview:view];
[view release];
}
if you want to push a view controller then you need to look up navigation controllers and the method pushViewController:animated:

This is the common code u can use to go on next view or go on back, Just change button name which u want to press and the view name on which u want to go. For example on back button press u want to go on library view then write this code -
-(IBAction)backButtonPressed:(id) sender
{
libraryView *libraryview=[[libraryView alloc] initWithNibName:nil bundle:nil];
libraryview.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:libraryview animated:YES];
[libraryview release];
}
Let me know whether ur problem is solved or not.

Related

Dealing with views and some other things

I'm study iOS development. Now playing with views. I don't use interface builder at the moment, because I prefer to understand how all stuffs works under the hood. This is why I create my views UI elements etc programmatically. However.
Here is how my project looks like.
I have a class called RootViewController. I use that class as a window rootViewController.
Within that class I have a logic which load/unload views. Here is some code.
RootViewController.m
-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view = view;
self.view.backgroundColor = [UIColor lightGrayColor];
// create FirstViewController button
UIButton *firstViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstViewButton.frame = CGRectMake(20, 60, 280, 50);
firstViewButton.tag = 1;
[firstViewButton setTitle:#"Load First view" forState:UIControlStateNormal];
[firstViewButton addTarget:self
action:#selector(loadViewControlllers:)
forControlEvents:UIControlEventTouchUpInside];
// crate SecondViewController button
UIButton *secondViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
secondViewButton.frame = CGRectMake(20, 140, 280, 50);
secondViewButton.tag = 2;
[secondViewButton setTitle:#"Load Second view" forState:UIControlStateNormal];
[secondViewButton addTarget:self
action:#selector(loadViewControlllers:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:firstViewButton];
[self.view addSubview:secondViewButton];
[self.view release];
}
-(void)loadViewControlllers:(UIButton *)sender
{
if ([sender tag] == 1) {
if(firstViewController == nil) {
firstViewController = [[FirstViewController alloc]
initWithNibName:#"FirstViewController"
bundle:nil];
[self.view addSubview:firstViewController.view];
}
} else {
if(secondViewController == nil) {
secondViewController = [[SecondViewController alloc]
initWithNibName:#"SecondViewController"
bundle:nil];
[self.view addSubview:secondViewController.view];
}
}
}
The first and the second view controllers contain same code
-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view = view;
self.view.backgroundColor = [UIColor redColor];
UIButton *remove = [UIButton buttonWithType:UIButtonTypeRoundedRect];
remove.frame = CGRectMake(20, 60, 280, 50);
[remove setTitle:#"Remove First view from superview" forState:UIControlStateNormal];
[remove addTarget:self
action:#selector(removeFromView:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:remove];
[self.view release];
}
-(IBAction)removeFromView:(id)sender
{
[self.view removeFromSuperview];
}
So far, so good.
Here is where my questions begin.
1) Is this the correct approach to show/remove views. One controller (rootViewController in my case) to rule them all ?
2) Could you suggest me a way to pass data between first, second and root views ? Lets say a firstViewController is loaded. That controller contains textfield and a button. What I want is when a user type something in a textfield and a button is pressed the data in that textfield to be able to read by rootViewController and secondViewController
3) The applications works well. No crashes etc. The problem is that when neither of first or second controller are called once they can't be loaded again.
When I press a button nothing is happens.
Last but not lest, sorry about my language.
1) Is this the correct approach to show/remove views. One controller
(rootViewController in my case) to rule them all ?
It's OK. However, if the firstViewController and the secondViewController share exactly the same logic, then they are actually one class, and it is generally safe for you to remove the redundancy.
2) Could you suggest me a way to pass data between first, second and
root views ? Lets say a firstViewController is loaded. That controller
contains textfield and a button. What I want is when a user type
something in a textfield and a button is pressed the data in that
textfield to be able to read by rootViewController and
secondViewController
You can use a singleton instance to hold the shared state(, the model in the MVC pattern), but it is not recommended.
IMHO, you can use an Observer model with Key-value-observing pattern. See Apple's guide here. Or you can post NSNotification(s) from one controller to notify any other controllers that is interested in that type of notifications. See [guides here].2
3) The applications works well. No crashes etc. The problem is that
when neither of first or second controller are called once they can't
be loaded again. When I press a button nothing is happens.
In your code above, the subviews were only added when the controller ivars were nil. Try to fix that logic.

Navigation Back Button does not show,how to add back button to UINavigationItem

I add UINavigationBar via Library to view. I also add UINavigationItem to this NavigationBar.
In viewController.m,I added the following code but back button doesn't show.
self.navigationItem.title = #"List";
self.navigationItem.backBarButtonItem.title = #"Back";
Try this:
self.navigationItem.leftItemsSupplementBackButton = YES;
or
navigationItem.leftItemsSupplementBackButton = true
If you customize your navigation controller with UIBarButtonItems the framework removes the back button by default, since it assumes you are customizing the Navigation bar. Use that line to add the back button in again.
self.navigationItem.backBarButtonItem will be shown only after your have pushed another one view to navigation stack, controlled by self.navigationController, if no left button on navigation bar is displayed.
From Apple docs:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
Try this code,
**To hide your default back button use this,**
self.navigationItem.hidesBackButton = YES;
UIImage* image = [UIImage imageNamed:#"back.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* backbtn = [[UIButton alloc] initWithFrame:frame];
[backbtn setBackgroundImage:image forState:UIControlStateNormal];
[backbtn setShowsTouchWhenHighlighted:YES];
[backbtn addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];
[self.navigationItem setLeftBarButtonItem:backButtonItem];
[backButtonItem release];
[backbtn release];
Action Event for back button:
-(IBAction)goBack{
//ur code here
}
Try this code may be it's help to u
UIButton *moreButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[moreButton1 setImage:[UIImage imageNamed:#"left_arrow.png"] forState:UIControlStateNormal];
//[moreButton setImage:[UIImage imageNamed:#"Button_OtherInfo_Active.png"] forState:UIControlStateHighlighted];
[moreButton1 addTarget:self action:#selector(backClick) forControlEvents:UIControlEventTouchUpInside];
[moreButton1 setFrame:CGRectMake(0, 0, 50,30)];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:moreButton1];
put this code in viewDidLoad
-(void)backClick
{
[self.navigationController popViewControllerAnimated:YES];
}
This is a bit old, but in case someone is looking for an answer...
The back button shows up automatically, but the current view (not the one being pushed) needs to have a title. eg:
- (void)viewDidLoad
{
self.title = #"Home";
[super viewDidLoad];
}
Then, when you push a view onto the view stack with
[self.navigationController pushViewController:aViewController animated:YES];
the back button shows up. No need to mess with UINavigationItem or UINavigationBar. Use those to customize the navigation bar. Take a look at the example project called NavBar, part of xcode.

Show a subview when button is pressed. (Something like view which appears when operating system Windows "start" button is pressed

I want to show a subview that will contain list of menu when a UIbutton is pressed. Should I use vertical SegmentControl?
Thanks for any help in advance
You don't have to use vertical TabbarViewController.
First of all, go to the interface builder and drag a Button, connect the Button to an action method you declare in the .m file
let's said that the method is
- (IBAction) btnHandler :(id)sender {
}
then declare a new view and add it to your main view
so the btnhandler method will look like this
- (IBAction) btnHandler :(id)sender {
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 300)];
//here add what ever you want to the new created view using the
//[subview addSubview:(your Componant)];
[self.view addSubview:subview];
}
please forgive me if i made a mistake, this is not tested code. i write it now in the browser.
If its an iPad application, maybe consider calling a UIPopOverController
You could use an Action Sheet if you're aiming for a native iOS look and feel.
Agree with the previous answer that a pop over is a great option if this is an iPad app.
You could go full custom of course with your own viewController which you can animate in from the top or bottom depending on your need.
Option 1:
Design the view in your nib.
Option 2:
Have a separate view controller
You can use addSubView method to add the view when you press the UIButton
Try below code that will help you.
- (void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
[super viewDidLoad];
}
-(IBAction)buttonPressed:(id)sender
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 200, 300, 100)];
view.backgroundColor = [UIColor clearColor];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
}

setToolbarItems issue

I added toolbar to on screen of navigation based application using the following code
//Create an array to hold the list of bar button items
NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];
//Add buttons
//load the image
UIImage *buttonImage ;
buttonImage = [UIImage imageNamed:#"test.png"];
//create the button and assign the image for window width and level
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(WWL:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//create a UIBarButtonItem with the button as a custom view
WindowWidthZoom = [[UIBarButtonItem alloc] initWithCustomView:button ];
[items addObject:WindowWidthZoom ];
[self setToolbarItems:items];
[[self navigationController] setToolbarHidden:NO animated:NO];
but when leave the screen , I noticed that the toolbar didn't disappear in the other screens , any suggestion how to hide it before leaving this screen to avoid its presence in other screens , and how to change its color to black
You might add the following to the -viewWillAppear method of all your other view controllers:
[self.navigationController setToolbarHidden:YES animated:NO];
In the view controller where you want the toolbar to appear, make sure you setToolbarHidden to NO also in the -viewWillAppear method. Doing so in the -viewDidLoad method is not enough because this method is not called every time a view appears. For example, when you hit the Back button of a navigation controller and return to a previous view controller, since that view controller has already been loaded, it may not need to load again (and so -viewDidLoad will not be called.)
Please change your last line code:
[[self navigationController] setToolbarHidden:NO animated:NO];
to:
[self setToolbarHidden:NO animated:NO];
And make sure call it in:
- (void)viewDidAppear:(BOOL)animated;

How to create a new UIView programmatically in cocos2d?

I want to create a new UIView on button click programmatically in cocos2d. Kindly help me with some sample code. Thanks in advance.
You have to create a button like-
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(30, 70,100,38); //set frame for button
[myButton setTitle:#"subview" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
Or you can use CCMenu as a button.
And then write the event handling function-
-(void)buttonClicked:(id)sender
{
UIView *myview=[[UIView alloc] initWithFrame: CGRectMake(0, 0,320,480)];
myview.backgroundColor=[UIColor redColor];
[[[CCDirector sharedDirector] openGLView] addSubview:myview];
[myview release];
}
}
As you wrote above, the class is called UIView. It's really simple to create a new View. Take a look at this code snippet.
-(void)youButtonAction:(id)sender {
//This method is executed as soon as you button is pressed.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[anotherView addSubview:newView]; //Add the newView to another View
[newView release];
}
I hope you got the point. At first you have to create the view, than add it to another view and in the end release it. ;-)
Sandro Meier