Rogue Zombie on dealloc of ViewController - iphone

In Xcode, start a new master-detail project. Call it 'Test'.
Add a new 'File' to it. Make it a UIViewController file with XIB. Call it TestViewController.
Modify your MasterViewController code in the insertNewObject: method to say this:
-(void)insertNewObject:(id)sender
{
TestViewController *initViewController = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
[self.navigationController pushViewController:initViewController animated:YES];
[initViewController release];
}
Now add a dealloc method into TestViewController.m and simply call [super dealloc];
Put a breakpoint here and run the app. All should run OK.
However, if you Enable Zombie object, you may get a *** -[TestViewController class]: message sent to deallocated instance 0x7484f20 error when stepping over [super dealloc].
Do you get this as well? I'm on iOS6.0 simulator and came across it trying to debug an issue which has led me to this.
Thoughts appreciated. Is it an iOS bug? Or a simulator bug?
Added TestViewController Code for 'clarity'
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc
{
// Zombie here???
[super dealloc];
}

I guess that your TestViewController has outlets and properties. If any of those properties/outlets are over released it will cause this error. Make sure that [super dealloc] is the last line on you dealloc method too

Related

viewDidLoad not Called for UIViewController with UIScrollView and UIPageControl

I am trying to add a page with a UIScrollView and UIPageControl to my app. I am making the call to the new class from a UITableView as below.
MultiPageViewController *mpvc = [[MultiPageViewController alloc]initWithNibName:#"MultiPageViewController" bundle:[NSBundle mainBundle]];
[[self navigationController]pushViewController:mpvc animated:YES];
The viewDidLoad function is never called, thus my UIScrollview and UIPageControl are never initialized. When I go along further and call viewWillAppear these objects are both still nil. Further the view never appears only a white screen. Below is the code from initWithNibName and viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(#"initWithNibName returned self != null");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"View did load called");
}
Thanks for your help in advance.

How to create viewController in PhoneGap to work with main view

I want to open file with [UIDocumentInteractionController presentPreviewAnimated];
I've created view controller
#interface FileViewController : UIViewController <UIDocumentInteractionControllerDelegate>
#end
#import "FileViewController.h"
#interface FileViewController ()
#end
#implementation FileViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (UIViewController *) documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And trying to preview the file
UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
vController = [[FileViewController alloc] init];
controller.delegate = vController;
[controller presentPreviewAnimated:YES];
And get error:
Warning: Attempt to present <QLPreviewController: 0x1e56e0a0> on <FileViewController: 0x1ec3e000> whose view is not in the window hierarchy!
I may set root view controller
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:vController];
But I dont want to use new view. I need to use the PhoneGap's main view. How can I do this?
The problem is solved
CDVViewController* mainController = (CDVViewController*)[ super viewController ];
[mainController addChildViewController:vController];

shouldStartLoadwithRequest not getting called in subclass

I have a view controlled app. On my xib I have no webviews but I do have buttons that bring up classes that have webviews. So hit button one and a uiwebview pops up so on and so forth.
Now in one of my classes I pull up a remote webpage that has a link on it. I want to over-ride that button link with shouldStartLoadwithrequest. How ever this never seems to get called.
Right now my class is called Tenth.m and I have this code in my view controller
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#" Test spot 1");
NSString* scheme = [[request URL] scheme];
if ([#"didTap" isEqual:scheme]) {
// Call your method
NSLog(#" Test spot 2");
[self didTapButton1];
return NO;
} else {
NSLog(#" Test spot 3");
return YES;
}}
But I get nothing (none of my NSLogs). I have tried putting this into my Tenth.m class and still nothing. I have looked at other examples and even downloaded one but it only uses the viewcontroller and delegates classes. No third class.
I'm lost as to why its not getting called.
Here is my third class
#import "Tenth.h"
#implementation Tenth
- (void)awakeFromNib
{
[category10 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.mywebsite.com/api/didtap.php"]]];
}
- (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
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (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
remote page code
hello
Tenth.h file
#import <UIKit/UIKit.h>
#interface Tenth : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *category10;
}
-(IBAction)back;
#end
My first guess is that the Web view does not have its delegate set. In order for it to call
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
The class containing this method needs to be set as the webview's delegate. So somewhere in the code there should be a line similar to webView.delegate = <variable>, where the variable is the instance of the class which has implemented the shouldStartLoadWithRequest: method.
This is often "self" when the webView is a subview of a viewController's view. But the way you have described your application, I am not sure if it will be self or not, but it sounds like it will not be self, but the name of the instance of a different class.
Maybe try this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Remember to set web view delegate to self.
category10.delegate = self;
}

Unable to load view on button press iPhone

I am unable to load the next view on button press. I have created a button and attached the checkButtonPress action to the button in the IB. But when I press the button on the simulator the next view (ViewOne.xib) do not load. When I tried to debug the code; it is printing the NSLog(#"View One");written in the if statement below which means the code is reaching to that point but the next view is not loading. I have already created the ViewOne .h, .m .xib files. Following is the code written in check1ViewController.m implementation file:
#import "check1ViewController.h"
#import "ViewOne.h"
#implementation check1ViewController
-(IBAction) checkButtonPress: (id) sender{
int button = [sender tag];
if(button==1)
{
NSLog(#"View One");
ViewOne *tempObj = [[ViewOne alloc]initWithNibName:#"ViewOne" bundle:nil];
[self.navigationController pushViewController:tempObj animated:YES];
[tempObj release];
}
else if(button==2)
{
NSLog(#"View Two");
}
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (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 {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
- (void)dealloc {
[super dealloc];
}
#end
The code is running fine with no errors or warnings.
Any suggestions if I am missing any part in the IB or in the code to be written or do need to override any method in any of the file ... or did I misplace IBAction before any other method
Sorry but I am new to iPhone dev so not sure what I did wrong ?
Thanks in advance
You application is not having navigation controller so add next view on window for this make object of AppDelegate class and then access window to addView.
YourAppDelegate *obj=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[obj.window addSubView:viewController.view];

Memory Crash due to [super dealloc] of uiview controller

I am having a uiviewcontroller instance and when I am releasing it the dealloc method of it is called.
I have released some objects in dealloc method of that uiviewcontroller.
If I comment [super dealloc] the app is working fine but if don't it is crashing.
I think there is no problem with the releases that I am doing in that method, but if I do [super dealloc] it is crashing.
Can any one help me out with this?
Hard to tell from your post without more information, but does your dealloc method look like this?
- (void)dealloc {
[super dealloc];
self.someProperty = nil;
}
Because if it does, you're calling a setter method on a deallocated instance. You should always call [super dealloc] last:
- (void)dealloc {
self.someProperty = nil;
[super dealloc];
}
Not sure if that helps. Try posting what your dealloc method looks like if not. Hard to troubleshoot in the dark.
It's not possible to help you without more information. The code you described is perfectly fine. The problem is in some other part of your app.
You probably access the view controller after releasing it, so the problem is not the [super dealloc] but any other place in you application that accesses the view controller.
Maybe you are releasing the controller in the wrong place. That could be why your [super dealloc] in your ViewController.m is crashing
You shouldn't called [viewController release] until you want that controller to die. For example, if you have an application with just a viewcontroller you must not release it until the application ends. This is because that controller needs to stay alive all the time to control the view. If you have in your ApplicationDelegate something like this, it will crash:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIViewController *controller = [[UIViewController alloc] init];
[window addSubview:controller.view];
[controller release]; //this will crash
}
Instead of that you should place your viewcontroller in the header file (.h) and release it in the dealloc method:
- (void)dealloc {
[controller release];
[window release];
[super dealloc];
}
I hope this helps.