How to disappear uiview by a button click - iphone

I have created a unbutton programatically and made an action
{
UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton1.frame = CGRectMake(20, 20, 80, 44);
[myButton1 setTitle:#"Click Me!" forState:UIControlStateNormal];
[myButton1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton1];
}
and in IBAction i have a UIView as subview like this
- (IBAction)buttonClicked:(id)sender
{
CGRect myFrame = CGRectMake(0, 300, 100, 50);
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myView];
[myView release];
}
What i now want is subview have to disappear when the user second time press myButton1. How can i do that?

Do some correction in our code -
**Action like this -**
- (IBAction)buttonClicked:(id)sender
{
UIView *myView;
if([self.view viewWithTag:56])
{
[[self.view viewWithTag:56] removeFromSuperview];
}
else
{
CGRect myFrame = CGRectMake(0, 300, 100, 50);
myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor lightGrayColor];
[myView setTag:56];
[self.view addSubview:myView];
[myView release];
}
}

you can use [view removeFromSuperView] method to remove a view.

Change your buttonClicked: method to this.
- (void)buttonClicked:(id)sender
{
UIView *myView;
if(myView=[self.view viewWithTag:1234])
{
[myView removeFromSuperview];
}
else {
CGRect myFrame = CGRectMake(0, 300, 100, 50);
myView= [[UIView alloc] initWithFrame:myFrame];
[myView setTag:1234];
myView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myView];
[myView release];
}
[self.view setNeedsDisplay];
}
EDIT: It doesn't read well if you use IBAction and not use the Interface Builder.

Related

Add Button on my View

I'm creating a hidden form containing 3 buttons at the bottom of the parent view, by pressing another button, the view becomes visible, but those 3 buttons are not pressed, I don `t know what the problem is.
I add a button via code. Here is a method that is called when the button is pressed.
- (IBAction) addData:(id)sender
{
if(attachInfo==nil){
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
UIButton *attachImage = [UIButton buttonWithType:UIButtonTypeCustom];
//attachImage.enabled = YES;
[attachImage addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
[attachImage setFrame:CGRectMake(20, 20, 0, 0)];
[attachImage setImage:[UIImage imageNamed:#"add_photos"] forState:UIControlStateNormal];
[attachImage sizeToFit];
[attachInfo addSubview:attachImage];
UIButton *attachDoc = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:#selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachDoc setFrame:CGRectMake(120, 20, 80, 80)];
[attachDoc setImage:[UIImage imageNamed:#"add_docs.png"] forState:UIControlStateNormal];
[attachDoc sizeToFit];
[attachInfo addSubview:attachDoc];
UIButton *attachPlace = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:#selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachPlace setFrame:CGRectMake(220, 20, 80, 80)];
[attachPlace setImage:[UIImage imageNamed:#"add_place.png"] forState:UIControlStateNormal];
[attachPlace sizeToFit];
[attachInfo addSubview:attachPlace];
//[self.view addSubview:attachInfo];
[self.view addSubview:attachInfo];
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
}];
}
else
{
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y += 216;
attachInfo.superview.frame=frame;
}];
attachInfo=nil;
}
}
you need to add Action of buttons.
- (IBAction)first:(id)sender;
{
firstViewController * fvc=[FirstViewController alloc]init];
[FirstButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
- (IBAction)Second:(id)sender;
{
SecondViewController * svc=[SecondViewController alloc]init];
[SecondButton setHidden:False];
[self.navigationController pushViewController:svc animated:YES];
}
- (IBAction)Third:(id)sender;
{
ThirdViewController * tvc=[ThirdViewController alloc]init];
[ThirdButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
In the first button (attachImage), the height and the width is zero. So it should not be visible. For the other two buttons you haven't set the target. So those are not clickable.
for the first button set
[attachImage setFrame:CGRectMake(20, 20, 80, 80)];
and for the other two buttons add
//for the second button
[attachDoc addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
//for the third button
[attachPlace addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
-(void)createHiddenForm
{
hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0,100,320,300)];
[hidden setHidden:TRUE];
[self.view addSubview:hiddenView];
float posY = 50.0;
for (int i=0 ; i<3 ; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50,posY, 100, 50)];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[hiddenView addSubview:button];
posY=posY+60;
}
}
-(void)showHiddenView
{
hidden.hidden = FALSE;
[self.view bringSubviewToFront:hiddenView];
}
-(void)buttonClicked:(id)sender
{
UIButton *senderBtn = (UIButton *)sender;
NSLog(#"Sender Button's tag = %#",senderBtn.tag);
}
Hope it will help you.
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
From the above, u r adding the attachInfo Y position outside the view boundaries.
Button onclick won't work when added outside the view.
To make it work replace below line.
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
Instead of moving superview frame move attachInfo frame like this.
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.frame;
frame.origin.y -= 216;
attachInfo.frame=frame;
So that attachInfo view will be added to the super view boundaries with animation.

Calling view controller method from delegate class, having issues

I am calling view controller method from delegate class, the method is called but uitableview declared in that method does not call it delegate methods and a buttons and view also alloc in that view but nothing is shown.
Explaining from begining:
On navigation controller needed a button and to be shown on all views. So, i took that in delegate this way.
UIButton *btnMenuOpen = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuOpen.frame = CGRectMake(0, 15, 40, 56);
[btnMenuOpen setImage:[UIImage imageNamed:#"side_menu.png"] forState:UIControlStateNormal];
[btnMenuOpen addTarget:self action:#selector(menu) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btnMenuOpen];
-(void)menu
{
ViewController *viewC = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[viewC menu];
}
This is the view controller class:
-(void)menu
{
NSLog(#"menu opened");
self.mMenuView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 240, 480)];
self.mMenuView.backgroundColor = [UIColor grayColor];
self.mMenuView.autoresizingMask = 0;
[self.navigationController.view addSubview:self.mMenuView];
self.mSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 16, 220, 44)];
self.mSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.mSearchBar.backgroundImage = [UIImage imageNamed:#"slide_tab_button.png"];
self.mSearchBar.delegate = self;
[self.mMenuView addSubview:self.mSearchBar];
UIButton *btnMenuClose = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuClose.frame = CGRectMake(215, 19, 40, 44);
[btnMenuClose setImage:[UIImage imageNamed:#"side_menu.png"] forState:UIControlStateNormal];
[btnMenuClose addTarget:self action:#selector(menu_close) forControlEvents:UIControlEventTouchUpInside];
[self.mMenuView addSubview:btnMenuClose];
self.mMenuTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 61,
240, 420) style:UITableViewStylePlain];
self.mMenuTableView.delegate = self;
self.mMenuTableView.dataSource = self;
[self.mMenuView addSubview:self.mMenuTableView];
[self.mMenuTableView reloadData];
}
Now, nothing is displyed when this method is called, control goes through it but nothing happens, no delegates of table are called and neither other stuff works(button searchbar and uiview).
Please guide for the above. Thanks in advance.
Try to use of NSNotificationCenter like this
Register Notification in your ViewController.m
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(menu)
name: #"myMenuNotification"
object: nil];
}
And in your AppDelegate.m call that method using notification like
-(void)menu
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"myMenuNotification" object:nil];
}
I don't know how you are using. But I have checked code with some modification. it is working fine.
Your modified code is:
In AppDelegate method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[sampleViewController alloc] initWithNibName:#"sampleViewController" bundle:nil];
self.navController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
UIButton *btnMenuOpen = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuOpen.frame = CGRectMake(0, 15, 40, 56);
[btnMenuOpen setImage:[UIImage imageNamed:#"5.jpg"] forState:UIControlStateNormal];
[btnMenuOpen addTarget:self action:#selector(menu) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btnMenuOpen];
[self menu];
return YES;
}
-(void)menu
{
[self.viewController menu];
}
//In view Controller............
-(void)menu
{
NSLog(#"menu opened");
UIView *mMenuView;
UISearchBar *mSearchBar;
UITableView *mMenuTableView;
mMenuView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 240, 480)];
mMenuView.backgroundColor = [UIColor grayColor];
mMenuView.autoresizingMask = 0;
[self.navigationController.view addSubview:mMenuView];
mSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 16, 220, 44)];
mSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
mSearchBar.backgroundImage = [UIImage imageNamed:#"5.jpg"];
// mSearchBar.delegate = self;
[mMenuView addSubview:mSearchBar];
UIButton *btnMenuClose = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuClose.frame = CGRectMake(215, 19, 40, 44);
[btnMenuClose setImage:[UIImage imageNamed:#"side_menu.png"] forState:UIControlStateNormal];
[btnMenuClose addTarget:self action:#selector(menu_close) forControlEvents:UIControlEventTouchUpInside];
[mMenuView addSubview:btnMenuClose];
mMenuTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 61, 240, 420) style:UITableViewStylePlain];
// mMenuTableView.delegate = self;
// mMenuTableView.dataSource = self;
[mMenuTableView setBackgroundColor:[UIColor blueColor]];
[mMenuView addSubview:mMenuTableView];
[mMenuTableView reloadData];
}
Screenshot

How to add a navigation bar and UIButton to a UIView that slides from bottom

Below is the code where I show a UIView from under a toolbar. It is displayed by clicking a button on toolbar. Now I want to add a button to the UIView that pops up , so as to cancel it. If possible a button over navigation bar which makes the UI better?
Can anyone point me towards a tutorial or so?
-(void)likeButton:(UIBarButtonItem *)button
{
CGRect frame = CGRectMake(0, 480, 320, 190);
frame.origin.y = CGRectGetMaxY(self.optionsToolBar.frame)-frame.size.height;
bottomView.frame = frame;
[self.optionsToolBar.superview insertSubview:bottomView
belowSubview:self.optionsToolBar];
}
-(void)likeButton:(UIBarButtonItem *)button
{
CGRect frame = CGRectMake(0, 480, 320, 190);
frame.origin.y = CGRectGetMaxY(self.optionsToolBar.frame)-frame.size.height;
bottomView.frame = frame;
UIView *navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationView setBackgroundColor:[UIColor blueColor]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"close" forState:UIControlStateNormal];
[button addTarget:self action:#selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
[navigationView addSubView:button];
[bottomView addSubView:navigationView];
[self.optionsToolBar.superview insertSubview:bottomView
belowSubview:self.optionsToolBar];
}
- (void) closeView :(id)sender{
[[sender superView] removeFromSuperView];
}

Adding a view to UIScrollview

I have created a UIView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *image2 = [UIImage imageNamed:#"pinGreen_v1.png"];
UIImage *image1 = [UIImage imageNamed:#"PinDown1.png"];
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[self addSubview:btn1];
for(int i =1; i<20; i++){
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(40 * i, 40, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
}
return self;
}
With the above code I am drawing images in my view. I am loading this view from my view controller. I need to put this view inside a UIScrollview. Where should I create the scroll view? In the above view or the view controller where the above view is created.
The code inside my view controller is :
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[drawLines release];
I should be able to scroll through the images inside my view.
I would create the the scrollview in the viewcontroller and add the view to it.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[scroll addSubview:drawLines];
scroll.contentSize = CGSizeMake(self.view.frame.size.width*drawLines.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[drawLines release];
[scroll release];

How do I programmatically add a exit button to a UIView

When I created a UIView in my superview, I want to add a button to this view that act as an exit button.
So when pressed, it will remove this UIView from my superview, how should I do that?
The View and button in that view is created programmatically when a button is pressed
- (IBAction)skillButtonPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
[cancelButton setTitle:#"x" forState:UIControlStateNormal];
[myView addSubview:cancelButton];
[self.view addSubview:myView];
}
In your class that creates the view, add a function like this:
//tag for subview - make it unique
#define SUBVIEW_TAG 9999
- (void) handleExit
{
UIView * subview = [self.view viewWithTag:SUBVIEW_TAG];
[subview removeFromSuperview];
}
and when creating the subview do:
- (IBAction)skillButtonPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancelButton addTarget:self action:#selector(handleExit) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
[cancelButton setTitle:#"x" forState:UIControlStateNormal];
[myView addSubview:cancelButton];
[myView setTag:SUBVIEW_TAG];
[self.view addSubview:myView];
//don't forget to release if you are not using ARC!
}
Here is the code for you:
self->myButton = [UIButton buttonWithType:UIButtonTypeCustom];
self-> myButton = CGRectMake(60, 6, 180, 30);
[self-> myButton setTitle:#"MyButton" forState:UIControlStateNormal];
[self-> myButton setTextColor:[UIColor whiteColor]];
self-> myButton = 1;
[self-> myButton addTarget:self action:#selector(myButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self-> myButton];
Change the coordinates for your button placing
In myButtonTouchUpInside method implemetation,
[self removeFromSuperview];
Hope this is what you want..
You can even hide the view or [self removeFromSuperview];