My problem is that in ProductNote class UIButton Action I did initWithNibName Notes Class to show the popOver with a UITableView in Notes Class. I am fetching data from sqlite and then load it to UITableView in tableViewDidSelectRowAtindexPath:. I got the selectedNote and creates object of ProductNote Class to call selectedNote instance method that only set IBOutlet’s textview.text but its nil. Thats my problem, Below is the code please help me to knowing why i face this type of issue. I am using Xcode 4.3.3 and not using ARC. Manually I defined dealloc method on every ViewController
//**ProductNote.h Class****************************
#import <UIKit/UIKit.h>
@class Notes;
@interface ProductNote : UIViewController<UIPopoverControllerDelegate>
{
UIPopoverController *popOverController;
UITextView *txtmesssagenotes;
Notes *objNotes;
}
@property (retain, nonatomic) IBOutlet UITextView *txtmesssagenotes; //It is Connected to ProductNote.xib
@property (retain,nonatomic) UIPopoverController *popOverController;
@property (retain,nonatomic) Notes *objNotes;
-(IBAction)presentPopOver:(UIButton*)sender;
-(void)selectedNote:(NSString*)note;
@end
//ProductNote.m Class
@implementation ProductNote
@synthesize txtmesssagenotes,popOverController,objNotes;
-(IBAction)presentPopOver:(UIButton*)sender
{
self.objNotes=[[Notes alloc]initWithNibName:@"Notes"bundle:nil];
UIPopoverController *popOver=[[[UIPopoverController alloc]initWithContentViewController:objNotes]autorelease];
self.popOverController=popOver;
self.popOverController.delegate=self;
self.popOverController.popoverContentSize=objNotes.view.frame.size;
CGRect rect=[self.view convertRect:objNotes.view.frame fromView:self.view];
rect.origin.y+=110;
rect.origin.x+=23;
[self.popOverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:0 animated:YES];
//Then I get popOver with tableview
}
-(void)selectedNote:(NSString*)note
{
self.txtmesssagenotes.text=note;
//Here I am getting txtmesssagenotes=0X0 or nil. Using self or without self Please tell me the reason.
}
- (void)dealloc {
[txtmesssagenotes release];
[popOverController release];
[objNotes release];
[super dealloc];
}
@end
@interface Notes : UITableViewController
{
NSMutableArray *arrNotes;
NSString *selectedNote;
UITableView *tblNotes;
}
@property(nonatomic,retain)NSMutableArray *arrNotes;
@property(nonatomic,retain)NSString *selectedNote;
@property(nonatomic,retain)IBOutlet UITableView *tblNotes;
@end
//Actually I skip the other methods that makes larger program to read . other methods only used to fetch data from sqlite and fill up the arrNotes that is used to fill all rows in the tableview.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedNote=[self.arrNotes objectAtIndex:indexPath.row];
//Here self.arrNotes valid object and contains the data, and assigns NSString object to self.selectedNote
ProductNote *productNote=[[ProductNote alloc]init];
[productNote selectedNote:self.selectedNote]; //after calling selectedNote i goto to selectedNote implementation or definition above.
[productNote release];
}
-(void)dealloc
{
[arrNotes release];
[selectedNote release];
[tblNotes release];
[super dealloc];
}
Are you calling
selectedNote:before your view has loaded?Just because you created a view controller with a xib doesn’t mean that it loads all your subviews immediately.
The xib is parsed and all your views are created the first time that the view controller’s
viewproperty is asked for – this is usually just before it appears for the first time.Until then, all the
IBOutletconnections will benil.You need to store the text for your note as an
NSStringproperty of you view controller. Then, inside eitherviewDidLoadyou need to callself.txtmesssagenotes.text=self.note;–viewDidLoadis automatically called just after your view is loaded so you are guaranteed to have all yourIBOutlets set by then.You shouldn’t rely on your view objects to store the state of your app (in this case, the text of the note). If a low memory warning is received while your view controller isn’t visible then all your view objects will be deleted – they will only be recreated when your view controller becomes visible again – if you are storing your note string inside a UITextView then it might no be there the next time you ask for it 🙂 You should always store data in a property of your view controller (or somewhere else you control) and create your views using it.