UIView continues to exist after removeFromSuperview - iphone

I add a firstView in the AppDelegate :
#import "TestViewAppDelegate.h"
#import "MainViewController.h"
#implementation TestViewAppDelegate
#synthesize window;
#synthesize mainViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MainViewController *aController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
self.mainViewController = aController;
[aController release];
self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window makeKeyAndVisible];
[window addSubview:mainViewController.view];
}
- (void)dealloc {
[mainViewController release];
[window release];
[super dealloc];
}
Then I want to switch to the secondView :
#import "MainViewController.h"
#import "SecondViewController.h"
#implementation MainViewController
#synthesize mainViewController, secondViewController;
- (IBAction)viewSwitch
{
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
self.secondViewController = second;
[second release];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[mainViewController.view removeFromSuperview];
[self.view addSubview:secondViewController.view];
[UIView commitAnimations];
}
- (void)dealloc {
[mainViewController release];
[secondViewController release];
[super dealloc];
}
#end
And then the same thing for switching from secondView to firstView…
The problem is when I switch the view I thought to release is always visible and don't disappear.
Download the full code

[mainViewController.view removeFromSuperview];
[self.view addSubview:secondViewController.view];
What is "self" here? And why does the MainViewController class have a property named mainViewController? Exploring those questions will likely lead you to the answer here.

A better approach might be to create one top-level parent view controller that you add to the window. It should have a reference to both of your other view controllers and do the swapping instead. If you want your button inside each view to cause the swapping, you could just have your button IBAction for each post a notification that your top level view registers for and responds to.

Related

dismissing modalview is not working

i m presenting modalviewcontroller without using delegate protocol. But want to dismiss modalviewcontroller using delegate protocol.
Basically i m pushing modalviewcontroller like this
- (void)displayModalViewaction: (id) sender
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[Infoviewcontroller alloc] init];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[self.view addSubview:navigationController.view];
[_viewController release];
[navigationController release];
}
For dismissing modalview doing this
In the ModalViewController.h delegated protocol and method
#protocol ModalViewDelegate <NSObject>
-(void) dismissModalView:(UIViewController *) viewController;
#end
#interface Infoviewcontroller : UIViewController <ModalViewDelegate>
{
id<ModalViewDelegate> dismissDelegate;
}
#property (nonatomic, retain) id<ModalViewDelegate> dismissDelegate;
#end
In modalviewcontroller. m file
#synthesize dismissDelegate;
-(void) dismissModalView:(UIViewController *) viewController;
{
[self dismissModalViewControllerAnimated:YES];
}
#end
-(void) dismissView: (id)sender
{
[delegate dismissModalView:self];
}
-(void) dismissModalView:(UIViewController *) viewController;
{
[self.dismissModalViewController Animated:YES];
}
#end
But somehow it is not working when clicking on done button
UIButton* backButton = [UIButton buttonWithType:101];
[backButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Done" forState:UIControlStateNormal];
// create button item
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
// add the button to navigation bar
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];
Anyone have any clue that what i m missing in my code or what i m doing wrong. Help will be really appreciated.
Thanks
I would have liked to comment, but not enough privilege. Anyways AFAIK...
One does not 'push' a modal VC... it needs to be 'presented' like this..
[navigationController presentModalViewController:_viewController animated:YES];
Only a presented Modal VC will get dismissed on calling [self dismissModalViewControllerAnimated:YES];
OR, in case you really need to 'push' it.. then you need to 'pop' it to get back!
Hope it helps
Correct me if I'm wrong but wouldn't you want to define _viewController in your .h file and dismiss it with: [delegate dismissModalView:_viewController]; rather then dismissing self because self is not a view controller.
You are pushing onto the navigation's stack so no Modal was ever displayed you need to pop the view off the stack:
[self.navigationController popViewControllerAnimated:YES];
instead of:
[self dismissModalViewControllerAnimated:YES];

Switching View through a function

I'm working on an iPhone/iPad application and trying to switch views through a function call, not by a button. I've seen lots of sources switching views triggered by a button, but there's nothing that explains how to switch views triggered by a function. I tried to do this on my own, but it failed. Here's the code that I tried:
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.webview == nil) {
self.webview = [[MainViewController alloc] init];
/* webview initialized with storyboard */
if (self.view.superview == nil) {
[self.view insertSubview:self.webview.view atIndex:0];
}
[storyboard release];
}
}
This is viewDidLoad of the viewController.m. First, it shows WebView.
- (void)changeView {
self.normview = [[AlternateViewController alloc] init];
/* normview initialized with storyboard */
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.webview viewWillDisappear:YES];
[self.webview viewDidDisappear:YES];
[self.webview.view removeFromSuperview];
[self.view insertSubview:self.normview.view atIndex:0];
[self.normview viewWillAppear:YES];
[self.normview viewDidAppear:YES];
[UIView commitAnimations];
}
And when this changeView function (in a viewController.m) is called, It should change the view from webview to normview (actually, it works fine when the same code is triggered by a button). But when I call this function in other file (not viewController.m), such as
ViewController *viewcontroller = [[ViewController alloc] init];
[viewcontroller changeView];
It doesn't work. Anyone can give a clue to solve this or an alternative way? (ps. I'm testing on iPad.)
You can switch views with this function:
YourView *screen = [[YourView alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
You can set the modaltransitionstyle to your willings.
Don't forget to import your headerfile at the top of your class.
#import "YourView.h"

animating view change with uitabbarcontroller

I am wanting to animate my view change, however I am not sure where to put my animation since I have a UITabBarController controlling which view I am on and what I can switch too.. This is being declared in my appdelegate.m file as per the xcode template.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Or their is the area in each viewcontroller where everything gets loaded... Im thinking it might be better to initialize the animation in here.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Super", #"Super");
self.tabBarItem.image = [UIImage imageNamed:#"SuperIcon"];
}
return self;
}
As I have not done alot of animation with views before I am woundering where I could declare this peice of code that will hopefully animate my view change when a UITabBarButton is touched....
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{ [fromView removeFromSuperview]; [containerView addSubview:toView];
completion:NULL];
or have I missed the boat completely and is their somewhere else I should be doing this?
Try the following code in the viewWillAppear method
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
This worked for me for a tabBar controller based app.
Try that animation in code in viewWillAppear method

UIImagePickerControllerSourceTypeCamera is covered by parent view objects

I am creating a window-based application in XCode 4 (I am using window-based because I like how the universal apps are organized), but I am having trouble with the UIImagePickerControllerSourceTypeSavedPhotosAlbum. I tried stripping down the code as much as possible to avoid any external errors, but I want to have two views:
the parent view: threeeyesmain.h / m / xib
the sub view: threeeyescamera.h / m / xib
I will post the code I have so far below.
The main issue is, when I push the button to take a picture with the camera, it works and I can see the camera controls, and I can even take a picture with no problem. However, whatever objects that are in the parent view are covering the camera screen. (i.e. if I am pointing my camera over a picture of a flower, I can see the flower on the screen, but there are buttons and imageviews from the parent view overlayed on it. I hope that makes sense).
(Side note: The funny thing is, when I tried this before using a view based application, it seemed to work, but now using virtually the same code in a windows based application, I get these problems.)
Maybe I am just missing something really obvious. Any help would greatly be appreciated. Thanks!
threeeyesmain.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <MediaPlayer/MediaPlayer.h>
#interface threeeyesmain : UIViewController {
}
-(IBAction)switchtothreeeyescamera:(id)sender;
-(IBAction)back:(id)sender;
#end
threeeyesmain.m
#import "threeeyesmain.h"
#import <AVFoundation/AVAudioPlayer.h>
#import <MediaPlayer/MediaPlayer.h>
#import "threeeyescamera.h"
#implementation threeeyesmain
-(IBAction)switchtothreeeyescamera:(id)sender{
threeeyescamera *mythreeeyescamera = [[threeeyescamera alloc]
initWithNibName:#"threeeyescamera"
bundle:nil];
UIView *thecurrentView = nil;
thecurrentView = self.view;
UIView *thenextView = nil;
thenextView = mythreeeyescamera.view;
thenextView.alpha = 0.0;
[self.view addSubview:thenextView];
[UIView beginAnimations:#"fadeview" context:nil];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:thenextView];
thenextView.alpha = 1.0;
[UIView commitAnimations];
}
-(IBAction)back:(id)sender {
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#end
threeeyescamera.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
#interface threeeyescamera : UIViewController {
UIImageView *SetPhotoImageView;
UIImagePickerController *imgPicker;
UIButton *BackButton;
}
#property (nonatomic, retain) IBOutlet UIImageView *SetPhotoImageView;
#property (nonatomic, retain) IBOutlet UIImagePickerController *imgPicker;
#property (nonatomic, retain) IBOutlet UIButton *BackButton;
-(IBAction)setImage:(id)sender;
-(IBAction)back:(id)sender;
#end
threeeyescamera.m
#import "threeeyescamera.h"
#implementation threeeyescamera
#synthesize imgPicker, SetPhotoImageView, BackButton;
-(IBAction)setImage:(id)sender{
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
// if((UIButton *) sender == ChoosePhoto) {
// picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// } else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// }
[self presentModalViewController:picker animated:YES];
}
-(IBAction)back:(id)sender {
[UIView beginAnimations:#"fadeview" context:nil];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
UIView *theView = nil;
theView = self.view;
[UIView setAnimationDelegate:theView];
theView.alpha = 0.0;
[UIView commitAnimations];
[UIView setAnimationDidStopSelector:#selector(removeFromSuperview)];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
SetPhotoImageView.image = img;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSData *ImageData = UIImageJPEGRepresentation(img, 0.9);
[prefs setObject:ImageData forKey:#"ImageSpaceMiddle"];
[prefs synchronize];
SetPhotoImageView.image = [UIImage imageWithData:ImageData];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsEditing = NO;
self.imgPicker.delegate = self;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs objectForKey:#"ImageSpaceMiddle"]) {
NSData *imgData = [prefs objectForKey:#"ImageSpaceMiddle"];
SetPhotoImageView.image = [UIImage imageWithData: imgData];
}
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#end
[self.view bringSubviewToFront:viewname];

request for member view in something not a structure or union

I have this error when i build my application iphone:
request for member view in something not a structure or union on [CommuneSlider.view removeFromSuperview];
the code:
- (void) CommuneSelected {
CommuneDetailsViewController *com = [[CommuneDetailsViewController alloc] initWithNibName:#"CommuneDetailsViewController" bundle:nil];
UINavigationController *navig = [[UINavigationController alloc]
initWithRootViewController:com];
[self setCommuneDetails:(CommuneDetailsViewController *) navig];
[navig setNavigationBarHidden:YES];
[com release];
[navig release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.8];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
[CommuneSlider.view removeFromSuperview];
[self.window addSubview:[CommuneDetails view]];
[UIView commitAnimations];
}
need HELP
If this is the error line:
request for member view in something not a structure or union on [CommuneSlider.view removeFromSuperview];
It seems to indicate that CommuneSlider does not have a "view" member. The name of the variable makes me think it is a UIControl, not a sub-class of UIViewController (which would have a view property).
Are you sure you don't want something like:
[CommuneSliderController.view removeFromSuperview];
ok this is AzurGuideAppDelegate.h:
#class CommuneSliderController, AccueilViewController,CommuneDetailsViewController;
#interface AzurGuideAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
AccueilViewController *AccueilController;
CommuneSliderController *CommuneSlider;
CommuneDetailsViewController *CommuneDetails;
UINavigationController *navigationControl;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet AccueilViewController *AccueilController;
#property (nonatomic, retain) IBOutlet CommuneSliderController *CommuneSlider;
#property (nonatomic, retain) IBOutlet CommuneDetailsViewController *CommuneDetails;
- (void) goBack;
- (void) goFront;
- (void) CommuneSelected;
#end
and here the AzurGuideAppDelegate.m where i defined my method:
#import "AzurGuideAppDelegate.h"
#import "AccueilViewController.h"
#implementation AzurGuideAppDelegate
#synthesize window;
#synthesize AccueilController;
#synthesize CommuneSlider;
#synthesize CommuneDetails;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window addSubview:AccueilController.view];
[window makeKeyAndVisible];
}
- (void) CommuneSelected {
CommuneDetailsViewController *com = [[CommuneDetailsViewController alloc] initWithNibName:#"CommuneDetailsViewController" bundle:nil];
UINavigationController *navig = [[UINavigationController alloc]
initWithRootViewController:com];
[self setCommuneDetails:(CommuneDetailsViewController *) navig];
[navig setNavigationBarHidden:YES];
[com release];
[navig release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.8];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
[CommuneSlider.view removeFromSuperview];
[self.window addSubview:[CommuneDetails view]];
[UIView commitAnimations];
}
and my CommuneSliderController class:
#import "AzurGuideAppDelegate.h"
#import "CommuneSliderController.h"
#import "CoverFlowView.h"
#import "CoverViewController.h"
#define CVC_VIEW_TAG 999
#implementation CommuneSliderController
- (IBAction) goFront:(id) sender {
AzurGuideAppDelegate *main = (AzurGuideAppDelegate *)[[UIApplication sharedApplication] delegate];
[main goFront];
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
CoverViewController *cvc = [[CoverViewController alloc] init];
cvc.view.tag = CVC_VIEW_TAG;
[self.view addSubview:cvc.view];
}
If this were C++, I'd say "there's a pointer to a class, and you try to say pClass.foo instead of pClass->foo", or the type whose "view" variable you try to access is unknown due to some reason. Maybe this could help with Objective-C as well.