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]];
Related
I am trying to replicate the screen in the middle, namely the alert that is displayed to compose a new tweet. I have tried to duplicate it with a custom UIAlertView but that requires a lot of subclassing and what not, then I suspected it might just be a simple UIViewController that is displayed over the main view... professional thoughts from fellow iOS developers is needed.
Thank you.
In iPad:
You can do it with a UIViewController.
You need to design UI according to the above image.
Then you need to set the Modal Presentation Style to UIModalPresentationFormSheet
And present it using - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))
Like:
UIViewController *viewC = [[UIViewController alloc] init];
viewC.view.frame = //set the frame;
viewC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:viewC animated:YES completion:nil];
In iPhone:
You can do it with using a UIView.
You can use the following code for doing this:
Declare a property in your interface
#property (nonatomic, strong) UIView *views;
//Add the view
- (void) showView
{
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 35)];
UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(remove)];
toolbar.items = [NSArray arrayWithObject:bar];
_views = [[UIView alloc] initWithFrame:CGRectMake(0, -300, self.view.bounds.size.width, self.view.bounds.size.height/3)];
[_views addSubview:toolbar];
[_views setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:_views];
[UIView animateWithDuration:0.7
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
_views.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height/3);
}
completion:^(BOOL yes){
NSLog(#"YO");
}];
[UIView commitAnimations];
}
//Remove the view
- (void)remove
{
[UIView animateWithDuration:0.7
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
_views.frame = CGRectMake(0, -500, self.view.bounds.size.width, self.view.bounds.size.height/3);
}
completion:^(BOOL yes){
NSLog(#"YA");
}];
[UIView commitAnimations];
}
My first view contains a button and after pressing a button, i will end up with the second view. what I have done is
FirstViewController.m
- (void)pushToWishList {
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil ];
[UIView beginAnimations:#"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view addSubview:controller.view];
[UIView commitAnimations];
}
SecondViewController is an UIViewController such that :
SecondViewController.h :
#interface SecondViewController :
UIViewController
SecondViewController.m
self.navigationController.navigationBarHidden = NO;
self.navigationItem.hidesBackButton = NO;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
What is happening is the top navigation is disppearing and the UITableView in my second view can not be scrolled up and down. My program is crashed whenever I try to do this.
Please advice me on this issue. Any comments are welcomed here.
First off its easier to not addSubview: just use this code to turn your page:
- (IBAction)myCart:(id)sender; {
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
}
This will allow your tableView to scroll! Also, your method return type is void, if you are using a button to call this method, you need to change the return type to IBAction which works for buttons.
How to give animation to UIImageView.
I have a following code for animation .but not working for imageview
MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:#"image.png"];
MyImage.userInteractionEnabled = YES;
[self.view addSubview:MyImage];
[UIView beginAnimations : nil context : nil];
[UIView setAnimationCurve : UIViewAnimationCurveLinear];
[UIView setAnimationDuration : 1.00];
UIView setAnimationTransition : UIViewAnimationTransitionFlipFromLeft forView : MyImage cache : YES];
[UIView commitAnimations];
Anybody have idea let me know ..Thanks
UIView setAnimationTransition : UIViewAnimationTransitionFlipFromLeft forView : MyImage cache : YES];
Loose [ before the code? Maybe it's the type mistake.
And I think you may like this one, block-based animation is better ;) :
[UIView transitionFromView:self.view
toView:MyImage
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
And here's code snippet that works well:
MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:#"NTCHomeMainPic.png"];
MyImage.userInteractionEnabled = YES;
// [self.view addSubview:MyImage]; // You don't need to add it as subview, but never mind
[UIView transitionFromView:self.view
toView:MyImage
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
Edit:
You need to create a new view and add the MyImage to it. New version below:
UIView * newView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[newView setBackgroundColor:[UIColor whiteColor]];
UIImageView * MyImage=[[UIImageView alloc] initWithFrame:CGRectMake(10, 57, 220,140)];
MyImage.image=[UIImage imageNamed:#"NTCHomeMainPic.png"];
MyImage.userInteractionEnabled = YES;
[newView addSubview:MyImage];
[UIView transitionFromView:self.view
toView:newView
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
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 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];