I've the following code in the main.m class file
-(void)switchView:(UIView *)view1 toView:(UIView *)view2{
[UIView beginAnimations:#"Animation" context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
[window addSubview:view2];
[UIView commitAnimations];
[view1 release];
}
I call this method from external files in this way:
-(IBAction)menuChangeLanguage:(id)sender{
main *delegate = (main *)[[UIApplication sharedApplication] delegate];
viewController *newView = [[viewController alloc] initWithNibName:#"newController" bundle:nil];
[delegate switchView:self.view toView:newView.view];
}
In this way I create a new view and switch with the existing one. Even if I've put the [view1 release] command in the method I call, I can see that the allocation of memory increases as I switch between the 2 views. What am I doing wrong?
If I use the analyze instrument it says Potential leak of an object stored into 'newView'. How can I avoid this?
The problem is that you are trying to remove the view from its viewcontroller. UIviewController has a strong reference to its view, so even if you remove from its superview, the view is retained by the viewcontroller.
take a look at the Apple UIViewController Class reference
Related
I got strange behaviour when setting the rootViewController programatically. I am using xib's only and here are scenarios of what I already tried.
When I use this code, there is a small blink of black screen before it loads VC correctly.
- (void)setRootVC:(UIViewController *)viewController {
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
[UIView transitionWithView:window
duration:0.0
options:UIViewAnimationOptionTransitionNone
animations:^{ window.rootViewController = viewController; }];
}
When I use different function, it eliminates the blink, but there's another strange behaviour. I got bunch of textfields in the new VC and I am setting one of them to becomeFirstResponder in viewDidLoad method, but when the VC loads, the textFieldDidEndEditing is called, which is totally strange. Here's the code.
- (void)setRootVC:(UIViewController *)viewController {
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
[UIView transitionFromView:window.rootViewController.view
toView:viewController.view
duration:0.0
options:UIViewAnimationOptionTransitionNone
completion:^(BOOL finished){
window.rootViewController = viewController;
}];
}
I am restricted with objective-C, so swift solutions will not be helpful. Thanks for the replies.
The blink is caused due to you are setting the rootViewController in completion where is it should be in animation block, so the code below might be helpful for you..
[UIView transitionWithView:self.window
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^{ self.window.rootViewController = viewController; }
completion:nil];
Instead of using UIViewAnimationOptionTransitionNone you might want to add some transition effect so, you can use UIViewAnimationOptionTransitionCrossDissolve instead, it might look better..
Hope it helps.
Cheers.
I've searched and searched for the answer to this issue and the solutions i've found doesn't seem to work, so I guess i'm doing something wrong: i want to call this method
-(void)customFade:(UILabel *)theLabel withDelay:(float)delayAmount {
[UIView animateWithDuration:0.5
delay:delayAmount
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[theLabel setAlpha:1.0f];
[theLabel setAlpha:0];
}completion:nil];
}
from a VC called View Controller to another but it isn't being recognized despite the imports... here's what i'm trying on the desired VC:
- (void)viewDidLoad {
[super viewDidLoad];
ViewController *ViewController = [[ViewController alloc] init];
[ViewController customFade:_label withDelay:0.3];
}
the warning says "no visible #interface for "ViewController" declares the selector alloc
Can someone help me out? I'm kind of new at this, thank you
such an error usually happens when you have mistyped the name of the method, or that method doesn't belong to that class at all and doesn't exist in your class.
and ensure once your VC is subclass with UIViewController
#interface ViewController : UIViewController
change your instance object type and check once
ViewController *vc = [[ViewController alloc] init];
[vc customFade:_label withDelay:0.3];
and change the animation like
[UIView animateWithDuration:0.5
delay:delayAmount
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[theLabel setAlpha:1.0f];
}completion:{
[theLabel setAlpha:0];
}];
for more information you can get the reference from here
What i want to achieve is: after tapping a small view with some data i want to make it full screen and possibly make it to be a new vc.
For now I animate uiview to full screen with great success, but whole logic of this view is in it's "parent".
Is it possible to animate viewcontroller out of the uiview which is similar (for. eg. like in LayoutTransitions in Android SDK)?
Sample code of my uiview to full screen using autolayout:
sender.view.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.2
animations:^{
sender.view.frame = self.view.window.bounds;
} completion:^(BOOL finished) {
[((CSTicketView*)sender.view) showMenu];
}];
[[UIApplication sharedApplication]
setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationFade];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
Let's move your sender view to Window as like below,
AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:sender.view];
[UIView animateWithDuration:0.2
animations:^{
sender.view.frame = self.view.window.bounds;
} completion:^(BOOL finished) {
[((CSTicketView*)sender.view) showMenu];
}];
Absolutely possible.
You can use the new view controller transitions model in iOS7.
I would definitely recommend a few resources to check on top of my basic explanation:
this
and this
I have some sample code for a demonstrator of this here
Ultimately you make a new view controller for the view you are transitioning to, and you also make an NSObject subclass that conforms to UIViewControllerAnimatedTransitioning which contains the code to transition between them. Sounds complex but if you watch the video I linked to and read the other reference it'll make total sense.
This is typical question and possibly duplicated, but..
There is iPad app which has UINavigationBar and UITabBar. I have created a button on navigationBar which must show appinfoViewController.
When I present it navigationBar and tabTab are still available to tap. But I would like to show appinfoViewController to full app's main screen size (like modal)
This is my code:
- (void)showInfo
{
AboutViewController *about = [[AboutViewController alloc] init];
[about.view setFrame: self.view.frame];
[about.view setAlpha:0.0];
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
[about.view setAlpha:1.0];
[self.view addSubview:about.view];
[self addChildViewController:about];
[about didMoveToParentViewController:self];
}
completion:^(BOOL finished){
}];
}
How to present appinfoViewController to full screen?
May be it's possible to present it modally but without BLACK background?
As suggested in the comments, using
[self presentViewController:about animated:YES completion:nil]
would get rid of the nav bar and the tab bar. If you need to keep them, you need to use
[self.navigationController pushViewController:about animated:YES];
EDIT:
In order to have the user interaction disabled everywhere except for your about view, it's slightly trickier: first off, you need to have all of your UI elements embedded in a view that is not the main view of your presenting view controller.
Let's say you have only a button (the "show about" button), you wouldn't just place it in your main view, but you would use another view (let's call it "outer view") that is just as big as the view controller's view and where you place the button (along with any other ui element you might have). You also need an outlet to this outer view. Then write a method such as:
-(void)userInteractionEnabled:(BOOL)enabled
{
self.navigationController.navigationBar.userInteractionEnabled = enabled;
self.tabBarController.tabBar.userInteractionEnabled = enabled;
self.outerView.userInteractionEnabled = enabled;
}
Alternatively you could simply disable every "interactive" outlet instead of outerView. So if, for example, you have 2 textviews, 3 buttons and one UIPickerView, you would set userInteractionEnabled = enabled for each of those outlets (instead of doing it only for the parent view).
Now, in your showInfo method you can have:
CGRect frame = CGRectMake(50, 50, 200, 200); //Use whatever origin and size you need
about.view.frame = frame;
[self.view addSubview:about.view];
[self userInteractionEnabled:NO]
And in your btnClose method you can just put:
[about.view removeFromSuperview];
[self userInteractionEnabled:YES];
I hope this helps, let me know if this is what you needed!
P.S. Maybe you're already aware of this, but there is a class UIPopoverController, only available for iPad's apps, that would pretty much do all of this for you. You can find a tutorial on how to use it here.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
AboutViewController *about = (AboutViewController *)[storyBoard instantiateViewControllerWithIdentifier:#"about"];
[self.navigationController pushViewController:about animated:YES];
You can add viewcontroller view layer directly to presenting viewcontroller view.
Code should be look like --
AboutViewController *about = [[AboutViewController alloc] init];
[about.view setFrame: self.view.bound];
[about.view setAlpha:0.0];
[self.view addSubview:about.view];
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
[about.view setAlpha:1.0];
}
completion:^(BOOL finished){
}];
I find myself in need of access to a viewcontroller from its view.
Here is the method
-(void)changePageView:(UIViewController*)newviewcont withtransitiontype:(int)t andtransitionspeed:(int)s
{
//Remove whatever view is currently loaded at index 0, this index is only to be used by "page" views
UIView *oldview = [self.view.subviews objectAtIndex:0];
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:s];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[newviewcont viewWillAppear:YES];
//[oldview viewWillDisappear:YES];
[oldview removeFromSuperview];
[self.view insertSubview:newviewcont.view atIndex:0];
//[oldview viewDidDisappear:YES];
[newviewcont viewDidAppear:YES];
}
Basically, I am trying to write a generic view switch method that is called by the root controller to swap out subviewcontorllers views from the rootcontrollers view.
I pass in a subviewcontroller and am able to remove the current subview. But in order to do proper view switching animation i need access to the current views view controller. Is this the wrong approach and can it be done?
I added a member to the rootcontroller that hold onto the current sub view controller (currentController) and refers to it when a controller swap is done
-(void)changePageView:(UIViewController*)newviewcont withtransitiontype:(int)t andtransitionspeed:(int)s
{
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:s];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[newviewcont viewWillAppear:YES];
[self.currentController viewWillDisappear:YES];
[self.currentController.view removeFromSuperview];
[self.view insertSubview:newviewcont.view atIndex:0];
[self.currentController viewDidDisappear:YES];
[newviewcont viewDidAppear:YES];
[UIView commitAnimations];
self.currentController = newviewcont;
}
The changeView() method belongs in the viewcontroller. It would solve you problem of having the view knowing about it's controller (which it shouldn't) and it makes more sense.
Also unless you are doing something fancy in changeView() that can't be done using the methods in a UIViewController object then you should just use it instead, if it is neccesary to implement your own view switching method then you can extend UIViewController instead of implemtning part of the view controlelr in your view.
my 2 cents :)
I believe your approach is wrong. You should look into UINavigationController I believe.