Application tried to push a nil view controller on target <UINavigationController - iphone

In my Iphone app I have this pb :Application tried to push a nil view controller on target <UINavigationController:
I have a view controller that contains a tableview and I want that if one row is selected an other view controller to be displayed.
Here is my code :
in RecherchePartenaireViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==3){
NSLog(#"RecherchePartenaireDistanceViewController...");
recherchePartenaireDistanceViewController = [[RecherchePartenaireDistanceViewController alloc] init];
self.recherchePartenaireDistanceViewController=recherchePartenaireDistanceViewController.recherchePartenaireViewController;
[self.navigationController pushViewController:recherchePartenaireDistanceViewController animated:YES];
[recherchePartenaireDistanceViewController release];
}
}
In RecherchePartenaireDistanceViewController.h :
#class RecherchePartenaireViewController;
#interface RecherchePartenaireDistanceViewController : UIViewController {
RecherchePartenaireViewController *recherchePartenaireViewController;
}
#property (nonatomic, retain) IBOutlet RecherchePartenaireViewController *recherchePartenaireViewController;
#end
and in RecherchePartenaireDistanceViewController.m
#import "RecherchePartenaireDistanceViewController.h"
#import "RecherchePartenaireViewController.h"
#implementation RecherchePartenaireDistanceViewController
#synthesize recherchePartenaireViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
[recherchePartenaireViewController release];
}
- (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 (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Where is the problem? I really can't see it. Please heeelp....

Try to use this:
- (void)dealloc
{
[recherchePartenaireViewController release];
[super dealloc];
}
instead of
- (void)dealloc
{
[super dealloc];
[recherchePartenaireViewController release];
}

I think I see what you're doing but I don't understand what you're trying to achieve! You have a ViewController that contains another view controller?
i.e. RecherchePartenaireDistanceViewController contains a RecherchePartenaireViewController
I assume that as it's an IBOutlet you're populating it using a xib file.
The only thing I can think of is that something is going wrong with the creation of your RecherchePartenaireDistanceViewController in the xib file.
What do you see in the console if you do this :
NSLog(#"controller : %#", recherchePartenaireDistanceViewController);
just before adding it to the navigation controller?
PS
What's the purpose of this line?
self.recherchePartenaireDistanceViewController=recherchePartenaireDistanceViewController.recherchePartenaireViewController;

if RecherchePartenaireDistanceViewController is with xib interface than use following statement while initializing,
recherchePartenaireDistanceViewController = [[RecherchePartenaireDistanceViewController alloc] initWithNibName:#"RecherchePartenaireDistanceViewController" bundle:nil];
than push it to navigation controller.

Related

i am not able to pass value from one view controller to another view controller

hi i have problem that i am not able to pass value from one view controller to another view controller through button i implement when i click on button other view appear on iphone screen but the value which i have set not dispay this is the button code
-(IBAction)save:(id)sender
{
nextview *admin = [[nextview alloc]init];
[self presentModalViewController:admin animated:YES];
if (admin.view)
{
admin.fetchname = name.text;
}
[admin release];
}
and this is the nextview.h file
#import <UIKit/UIKit.h>
#interface nextview : UIViewController
{
UILabel *getname;
NSString *fetchname;
}
#property (nonatomic,retain) IBOutlet NSString *fetchname;
#property (nonatomic,retain) IBOutlet UILabel *getname;
#end
and this is the nextview.m file
#import "nextview.h"
#import "ViewController.h"
#implementation nextview
#synthesize getname,fetchname;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (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
{
getname.text = self.fetchname;
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
The viewDidLoad method is called before you had a chance to assign your NSString value.
That's why you dont see the text in your UIButton.
Try this :
-(IBAction)save:(id)sender
{
nextview *admin = [[nextview alloc] init];
admin.fetchname = name.text;
[self presentModalViewController:admin animated:YES];
[admin release];
}
-(IBAction)save:(id)sender
{
nextview *admin = [[nextview alloc]init];
[self presentModalViewController:admin animated:YES];
if (admin.view)
{
admin.fetchname = name.text;
}
[admin release];
}
You release the instance of nextview right after you assign the name. That can't even work.
By the way, getname and fetchname are really bad chosen names for properties.
You can do something like this.
You can implement the below code in nextView.h
NSString *fetchData;
also property and synthesize this
#property(nonatomic, retain) NSString *fetchData;
implement this on button pressed code
-(IBAction)save:(id)sender
{
nextview *admin = [[nextview alloc] init];
admin.fetchData = name.text;
[self presentModalViewController:admin animated:YES];
[admin release];
}

Connecting Touch Up Inside event to File's Owner

I’m working through the Multiview Applications chapter of Apress’s Beginning iOS5 development and ran into an issue making a connection from a button on one of the views to File Owner. The general premise of the program is to use a root controller, BIDSwitchViewController to switch between two content views BIDBlueViewController and BIDYellowViewController. Each of the content views has a single button connecting the Touch Up Inside event to the File’s Owner. I had no problem making the connection in BIDBlueViewController, but when I attempt to make this connection in the BIDYellowViewController I’m unable to make a connection between the Touch Up Inside event and the File’s Owner icon.
The instructions in the book state
Drag a Round Rect Button from the library over to the view, using the
guidelines to center the button in the view, both vertically and
horizontally. Double-click the button, and change its title to Press
Me. Next, with the button still selected, switch to the connections
inspector drag from the Touch Up Inside event to the
File's Owner icon, and connect to the blueButtonPressed action method
I’m able to do this without any issues, however when I try complete this instruction for the Yellow View
Drag a Round Rect Button from the library over to the view, using the
guidelines to center the button in the view, both vertically and
horizontally. Double-click the button, and change its title to Press
Me. Next, with the button still selected, switch to the connections
inspector drag from the Touch Up Inside event to the
File's Owner icon, and connect to the yellowButtonPressed action method
The File Owner icon will not let me make the connection, as I drag from the Touch Up Inside event to the File Owner icon File Owner never highlights and I cannot make the connection.
Here is my code
The root controller interface
//
// BIDSwitchViewController.h
// ViewSwitcher
//
//
#import <UIKit/UIKit.h>
#class BIDYellowViewController;
#class BIDBlueViewController;
#interface BIDSwitchViewController : UIViewController
#property (strong, nonatomic) BIDYellowViewController *yellowViewController;
#property (strong, nonatomic) BIDBlueViewController *blueViewController;
- (IBAction)switchViews:(id)sender;
#end
The root controller implementation
//
// BIDSwitchViewController.m
// ViewSwitcher
//
#import "BIDSwitchViewController.h"
#import "BIDYellowViewController.h"
#import "BIDBlueViewController.h"
#implementation BIDSwitchViewController
#synthesize yellowViewController;
#synthesize blueViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.blueViewController = [[BIDBlueViewController alloc]
initWithNibName:#"BlueView" bundle:nil];
[self.view insertSubview:self.blueViewController.view atIndex:0];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)switchViews:(id)sender{
if (self.yellowViewController.view.superview == nil){
if (self.yellowViewController == nil) {
self.yellowViewController =
[[BIDYellowViewController alloc] initWithNibName: #"YellowView"
bundle:nil];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
if (self.blueViewController == nil){
self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:#"BlueView"
bundle:nil];
}
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex: 0];
}
}
- (void) didReceiveMemoryWarning {
// Release the view if it doesn't have a super view
[super didReceiveMemoryWarning];
// Release any cached data, images, etc, that aren't in use
if (self.blueViewController.view.superview == nil){
self.blueViewController = nil;
} else {
self.yellowViewController = nil;
}
}
#end
The blue content view controller interface
//
// BIDSwitchViewController.h
// ViewSwitcher
//
#import <UIKit/UIKit.h>
#class BIDYellowViewController;
#class BIDBlueViewController;
#interface BIDSwitchViewController : UIViewController
#property (strong, nonatomic) BIDYellowViewController *yellowViewController;
#property (strong, nonatomic) BIDBlueViewController *blueViewController;
- (IBAction)switchViews:(id)sender;
#end
The blue content view controller implementation
//
// BIDBlueViewController.m
// ViewSwitcher
//
//
#import "BIDBlueViewController.h"
#interface BIDBlueViewController ()
#end
#implementation BIDBlueViewController
- (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.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
The yellow content view controller interface
//
// BIDYellowViewController.h
// ViewSwitcher
//
#import <UIKit/UIKit.h>
#interface BIDYellowViewController : UIViewController
- (IBAction)yellowButtonPressed;
#end
The yellow content view controller implementation
//
// BIDYellowViewController.m
// ViewSwitcher
//
//
#import "BIDYellowViewController.h"
#interface BIDYellowViewController ()
#end
#implementation BIDYellowViewController
- (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.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end

Give out a NSMutableArray on a texView on another Viewcontroller in objective-C?

I try to save numbers from a textfield on one viewController in a NSMutableArray when I press a button on this viewContoller. (this is working now)
Then i want the numbers give out on a textview which is on a secondViewController but this dont work. When i want to give out the array on the first Viewcontroller it work fine.
Also i cant erase the NSMutableArray with a button on the SecondviewController.
The SecondviewController have the same class like the viewController.
Can someone show me how i can give out an array on a seconviewcontroller?
Hallo,
at the moment i have this:
//ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
NSMutableArray *textViewArray;
}
#property (strong, nonatomic) IBOutlet UITextField *textLable2;
#property (strong, nonatomic) IBOutlet UITextField *textLable1;
- (IBAction)setArrayWithCurrentNumber:(id)sender;
- (IBAction)returnToTextfield:(id)sender;
#end
//this the .m file:
#import "ViewController.h"
#implementation ViewController
#synthesize textLable2;
#synthesize textLable1;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
textViewArray = [[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setTextLable2:nil];
[self setTextLable1:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)setArrayWithCurrentNumber:(id)sender
{
NSString *string1 = self.textLable1.text;
[textViewArray addObject:string1];
NSMutableArray *array = [NSMutableArray arrayWithArray:textViewArray];
NSString *string2 = [array componentsJoinedByString:#" "];
self.textLable2.text = [NSString stringWithString:string2];
NSLog(#"%#",textViewArray);
}
- (IBAction)returnToTextfield:(id)sender
{
[textLable1 resignFirstResponder];
[textLable2 resignFirstResponder];
}
#end
If you're calling the second view controller from the first one you could set a property on the second one to hold the NSMutableArray or just send it on the initializer.
something like:
- (id)initWithArray:(NSMutableArray *)array {
if (self = [super init]) {
myArray = [array copy];
}
return self;
}
assuming your second view controller has declared NSMutableArray * myArray;
Edit: Adding some more code in here...
// I'll assume you use some kind of UINavigationController to show your content
- (void)showSecondViewController {
SecondViewController * vc = [[[SecondViewController alloc] initWithArray:yourMutableArray] autorelease];
[self.navigationController pushViewController:vc];
}

Showing ads on every view automatically in iPhone

I'm implementing Mobclix in my app, and when following the instructions, it tells us to properly release and dealloc the adview. It also tells us that when the view appears, you should call refreshAds and when it disappears you should pause refresh ads:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.adView resumeAdAutoRefresh];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.adView pauseAdAutoRefresh];
}
#implementation RootViewController
- (void)viewDidUnload {
[self.adView cancelAd];
self.adView.delegate = nil;
self.adView = nil;
}
- (void)dealloc {
[self.adView cancelAd];
self.adView.delegate = nil;
self.adView = nil;
[super dealloc];
}
#end
My question is that I have a lot of view controllers, so it would be inconvenient to implement these things on each view. Is there anyway I can call these methods in one place and have it work for all views?
What you can do is subclass UIViewController and have all your viewControllers to inherit these things.
So basically you could have AdsViewController.h wich will look like
#import <Foundation/Foundation.h?
#interface AdsViewController: UIViewController {
}
#property (nonatomic, retain) <#AdsViewType#> *adsView;
#end
and your AdsViewController.m will have to look pretty much like this
#import "AdsViewController.h"
#implementation AdsViewController
#synthesize adsView = _adsView;
- (id)init {
self = [super init];
if (self) {
_adsView = [[<#AdsViewType#> alloc] init];
_adsView.delegate = self;
}
return self;
}
- (void)viewDidUnload {
[self.adView cancelAd];
self.adView = nil;
[super viewDidUnload];
}
- (void)dealloc {
[self.adView cancelAd];
self.adsView = nil;
[super dealloc];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.adView resumeAdAutoRefresh];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.adView pauseAdAutoRefresh];
}
#end
After this have all your viewControllers to subclass this instead of UIViewController. Hope this helps.

Dismiss popover using UIbutton

I can't figure out why it doesn't work as it should when I try to dismiss a popover by clicking on a UIButton which itself is on a the popover to be dismissed, my project crashes...
- (IBAction) cancelButton: (id) sender{
//[self dismissPopoverAnimated:YES];
}
Above is my code for my UIButton
Dont dismiss the popover from within its self. Make a protocol that sends a message to its delegate to then dismiss it. For example, your popover view controller might look like this..
// MyPopoverViewController.h
#protocol MyPopoverDelegate <NSObject>
-(void)didClickCancelButton;
#end
#interface MyPopoverViewController : UIViewController {
}
#property (nonatomic, assign) id<MyPopoverDelegate> delegate;
-(IBAction)cancelButton;
#end
// MyPopoverViewController.m
#import "MyPopoverViewController.h"
#implementation MyPopoverViewController
#synthesize delegate;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
-(IBAction)cancelButton {
[self.delegate didClickCancelButton];
}
#pragma mark -
#pragma mark Rotation support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
#pragma mark -
#pragma mark Memory Management
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
And then you could use..
// ClassImplementingPopoverController.h
#import <UIKit/UIKit.h>
#import "MyPopoverViewController.h"
#interface ClassImplementingPopoverController : UIViewController <UIPopoverControllerDelegate, MyPopoverDelegate> {
UIPopoverController *myPopoverController;
}
#property (nonatomic, retain) UIPopoverController *myPopoverController;
#end
// ClassImplementingPopoverController.m
#import "ClassImplementingPopoverController.h"
#import "MyPopoverViewController.h"
#implementation ClassImplementingPopoverController
#synthesize myPopoverController;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
#pragma mark -
#pragma mark MyPopover delegate
-(void)didClickCancelButton {
if ([myPopoverController isPopoverVisible]) {
[myPopoverController dismissPopoverAnimated:YES];
[myPopoverController release];
}
}
#pragma mark -
#pragma mark UIPopoverController delegate
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
if (popoverController == myPopoverController) {
[myPopoverController release];
}
}
/* Use something like this to create your popover, just make sure you set the delegate to self so you can receive the messages
NSLog(#"Displaying Popover!");
MyPopoverViewController *detailViewController = [[MyPopoverViewController alloc] initWithNibName:#"MyPopoverViewController" bundle:nil];
[detailViewController setDelegate:self];
// Pass the selected object to the new view controller.
myPopoverController = [[UIPopoverController alloc] initWithContentViewController:detailViewController];
[detailViewController release];
myPopoverController.popoverContentSize = CGSizeMake(500.0, 150.0);
[myPopoverController setDelegate:self];
*/
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.myPopoverController = nil;
}
- (void)dealloc {
[myPopoverController release];
[super dealloc];
}
#end
you can use KVC accessing "popoverController" like
[self valueForKey:#"popoverController"]
But it is potentially rejected by AppStore if they detect your code is using their's "private API".
dismissPopoverAnimated is a property on the PopoverController itself, not your custom VC. You are showing an IBAction on 'self', which suggests you are inside your own class here. (Please post more surrounding code if that isn't correct.)
A VC can't get to its enclosing popover directly; you just need to keep a reference to it in the VC that created it so you can hit it with dismiss from there.
You can dismiss a Popover from inside itself, that doesn't crash.