I would like to pop-up a subview pop-up from one of the actions performed by a button placedon a tableview controller.
I initially added a sub-view(initally hidden below the screen) and later want to animate it to
animate and pop-up.
Here is the code for the button action
-(IBAction) finalShareVerse: (id) sender
{
NSLog(#"finalShare Button");
UIView *tempView;
CGRect tmpFrame;
tempView = [[[UIView alloc] initWithFrame:CGRectMake(0, 490, 320, 90)]
autorelease];
[tempView setBackgroundColor:[UIColor blackColor]];
[tempView setAlpha:.87];
[self.view addSubview:tempView];
tmpFrame = tempView.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
tmpFrame.origin.y=390;
self.view.frame=tmpFrame;
[UIView commitAnimations];
}
The problem is that now the parent controller gets animated and popped down instead of
the subview to be popped up.
Is there a way to specify the animation to trigger on only the sub view.
I am new to iPhone app dev.. Please advise.
If I understand correctly, the problem is the line:
self.view.frame=tmpFrame;
You are applying the change of position to the view rather than the tmpView
Related
I'm trying to execute a method that is my SecondViewController from my FirstViewController, and it works, the method is called cuz I see my message(NSLog(#"printSomething")) in the log console.
The problem is that in that method am trying to move a UIToolBar, but it doesn't.
If I call the method from his own viewController it works, the UIToolBar moves, but when I call it from another viewController...the method is executed, but the UIToolBar do nothing
FirstViewController.m
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Opciones"
style:UIBarButtonItemStyleBordered
target:secondViewController
action:#selector(showOptions)];
SecondViewController.m
-(void)showOptions{
NSLog(#"printSomething");
[self.toolBar setFrame:CGRectMake(0, 40, 320, 50)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[self.toolBar setFrame:CGRectMake(0, 180, 320, 50)];
[self.toolBar setUserInteractionEnabled:YES];
[UIView commitAnimations];
}
Thanks for ur replays.
Change your NSLog statement to this:
NSLog(#"self.toolbar is 0x%x",self.toolBar);
and see if the value is non-null. This is a very common problem - sending a message to a null object is not a runtime error in Objective-C.
I would like to switch between UIViews in a correct way.
This is how I currently do it :
I trigger a method when clicking on a button
[myButton addTarget:self
action:#selector(switchToMySecondView:)
forControlEvents:UIControlEventTouchUpInside];
In switchToMySecondView, I allocate my new UIViewController (mySecondViewController is a attribute of the current class) :
MySecondViewController* mySecondView = [[MySecondViewController alloc] initWithNibName:#"SecondViewXib" bundle:nil];
self.mySecondViewController = mySecondView;
[mySecondView release];
Add some stuff in mySecondViewController...
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
myLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
[mySecondViewController.view addSubView:myLabel];
Next I display it, using a UIAnimation :
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[mySecondViewController viewWillAppear:YES];
[self.view addSubview:mySecondViewController.view];
[mySecondViewController viewDidAppear:YES];
[UIView commitAnimations];
Finally, in my second view controller, I use the reverse method to switch back to my first view :
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.40];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
Ok. That works fine. I have no doubt there are memory leaks, but it's currently not my priority.
Here is my problem :
Each time I click on my button in the first view, I add a different UILabel to my second view (using the [sender tag] index in my aGlobalArray).
Each time this UILabel is added to my secondView, it overlays on the old UILabel, so I still can see the both UILabel.
UILabel is just an example. In my app I am also adding UIImages which overlay too.
I tried to write it in my secondView when switching back to my first view :
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[self release];
[UIView commitAnimations];
And my app is suddenly stopped after 2 or 3 switch/switch back, sometimes without ny message in the console, sometimes with a message saying I am releasing something not allocated.
What am I doing wrong ?
Any help would be greatly appreciated !
You shouldn't be recreating the UILabel in the second view controller. You could either create the label in your MySecondViewController * class and expose it as a property. Then you'd simply do the following whenever you want to switch to the second view:
mySecondViewController.theLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
Conversely, you could set a tag on the UILabel when you first create it and later retrieve the label using viewWithTag.
EDIT: When I say expose as a property, you'd create the label as normal in your interface:
#interface MySecondViewController : UIViewController {
UILabel *label;
}
#property (nonatomic, retain) UILabel *label;
The in the implementation file:
#implementation MySecondViewController
#synthesize label;
Then you'd create the label only once - i.e. in MySecondViewController ViewDidLoad or instead of your:
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
myLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
[mySecondViewController.view addSubView:myLabel];
you could do:
if (mySecondViewController.label == nil) {
mySecondViewController.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
[mySecondViewController.view addSubView:self.mySecondViewController.label];
}
mySecondViewController.label.text = [aGlobalArray objectAtIndex:[sender tag]];
I have a main view with four buttons, each time a button is clicked a subview is added. But when I click the button, and the subview comes up, I can´t pressed any of the other buttons. Does anyone have any suggestions? Here´s the code when a buttons is clicked:
newView = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
[newView addSubview: testArena.view];
[newView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)];
[UIView beginAnimations:#"animateArenaView" context:nil];
[UIView setAnimationDuration:0.4];
[newView setFrame:CGRectMake( 0.0f, 20.0f, 320.0f, 460.0f)];
[UIView commitAnimations];
[newView setUserInteractionEnabled:YES];
[self.view addSubview: newView];
Where newView is a UIView, and testArena.view is the class I´m getting the view I want displayed from.
Sounds like newView might be hiding/stopping the buttons from being pressed? Try sending it to the back, so it can't cover the buttons:
[self.view sendSubviewToBack:newView];
while you click on one button inside that you can disable all other buttons..
if(button1){
button2 userinteraction enable = no;
button3 userinteraction enable = no;
button4 userinteraction enable = no;
}
if(button2){
button1 userinteraction enable = no;
button3 userinteraction enable = no;
button4 userinteraction enable = no;
}
...
..
I have a view, and a button in this view. When I press this button I need open other view as a frame. Searching I found this post: iOS -- how do you control the size of a modal view controller?
I modify this and I doing this in a IBAction connected to a button:
View2Controller *screen = [[View2Controller alloc] initWithNibName:nil bundle:nil]; //Line add
UIView *myHalfView = [[UIView alloc] initWithFrame:screen.view.frame]; //Line modified by me
[self.view addSubview:myHalfView];
CGRect offScreenFrame = myHalfView.bounds;
offScreenFrame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.frame));
[UIView beginAnimations:nil context:nil];
myHalfView.center = CGPointMake(myHalfView.center.x, myHalfView.center.y - myHalfView.bounds.size.height);
[UIView commitAnimations];
[myHalfView release];
But when I press that button nothing happens. I've verified the code runs in debug it step by step.
Thanks.
Take a peak at this it's golden... read the comments in there as well.
http://humblecoder.blogspot.com/2009/04/iphone-tutorial-navigation-controller.html
Your View2Controller is initialized without a nibname?
I typically have a xib I built in Interface Builder in and do the following (have the view in the xib be whatever size you want by default if it doesn't change):
MyViewController* controller = [[MyViewController alloc]initWithNibName:#"MyViewController" bundle:nil];
[navigationController.view addSubview:controller.view];
//manipulate controller.view here
[controller release];
Thanks all for the replies. Finally I do this and works:
First in my IBAction:
View2Controller* modalViewController = [[[View2Controller alloc] initWithNibName:#"View2Controller" bundle:nil] autorelease];
[self.view addSubview:modalViewController.view];
Then in View2Controller.m in viewDidLoad:
[self.view setBackgroundColor:[UIColor clearColor]];
[UIView beginAnimations:nil context:nil];
[self.view setFrame:CGRectMake(0, 1024, 128, 600)];
[UIView setAnimationDuration:0.75f];
[self.view setFrame:CGRectMake(0, 404, 128, 600)];
[UIView commitAnimations];
I have the following view hierarchy
UITabBarController
- UINavigationController
- UITableViewController
When the table view appears (animated) I create a toolbar and add it as subview of the TabBar at the bottom of the page
and let it animate in with the table view. Same procedure in other direction, when the table view disappears.
It does not work as expected.
The animation duration is OK, but somehow not exact the same as the animation of the table view when it becomes visible
When I display the table view for the second time, the toolbar does not disappear at all and remains at the bottom of the
parent view.
What's wrong with it?
- (void)animationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
UIView *toolBar = [[[self tabBarController] view] viewWithTag:1000];
[toolBar removeFromSuperview];
}
- (void)viewWillAppear:(BOOL)animated
{
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 44, 0);
[[self tableView] setContentInset:insets];
[[self tableView] setScrollIndicatorInsets:insets];
// Toolbar initially placed outside of the visible frame (x=320)
UIView *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(320, 480-44, 320, 44)];
[toolBar setTag:1000];
[[[self tabBarController] view] addSubview:toolBar];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.35];
[toolBar setFrame:CGRectMake(0, 480-44, 320, 44)];
[UIView commitAnimations];
[toolBar release];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
UIView *toolBar = [[[self tabBarController] view] viewWithTag:1000];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.35];
[UIView setAnimationDidStopSelector:#selector(animationDone:finished:context:)];
[toolBar setFrame:CGRectMake(320, 480-44, 320, 44)];
[UIView commitAnimations];
[super viewWillDisappear:animated];
}
Have you tried just using the table view controller's toolbarItems property? UINavigationController will manage a toolbar for you, updating it with the toolbar items of its current topmost view controller; use the -setToolbarHidden:animated: method in your -viewWillAppear: and -viewWillDisappear: to control the visibility of that toolbar.