Please help me find the two leaks in this iOS code - objective-c

In my viewController.m I have this code:
self.movie = [[myMovie alloc]init];
self.movie.name = #"Iron man 2"; \\this line leaks
...
nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 200, 20)]; \\this line leaks
nameLbl.backgroundColor = [UIColor clearColor];
In viewController.h I have this code:
#interface ViewController : UIViewController
{
myMovie * movie;
UILabel * nameLbl;
}
#property (nonatomic, retain) myMovie * movie;
#property (nonatomic, retain) UILabel * nameLbl;
And myMovie.h:
{
NSString* name;
}
#property (nonatomic, retain) NSString* name;
myMovie.m:
#import "myMovie.h"
#implementation myMovie
#synthesize name, gross, desc;
-(void) dealloc
{
self.name = nil;
self.gross = nil;
self.desc = nil;
[super dealloc];
}
#end
Of course this is only the necessary code. I can't figure out why it is leaking. I don't know if this is the cause but my application crashes.

The line that's leaking is the one above: self.movie = [[myMovie alloc]init];
Change it to self.movie = [[[myMovie alloc]init] autorelease]; or add [self.movie release]; as the line immediately afterwards.

Related

Objective C Property Syntax

I have quick question regarding my code:
This is my Animal.h header file:
#import <Foundation/Foundation.h>
#interface Animal : NSObject
#property (nonatomic) int age;
#property (nonatomic, strong) NSString *name;
#property (nonatomic,strong) NSString *breed;
#property (retain, nonatomic) UIImage *image;
-(void) bark;
-(void)barkNumTimes: (int)numOfTimesToBark;
-(void)barknumTimes:(int)numberOfTimes loudly:(bool) isLoud;
-(int) ageInDogYears: (int)humanYears;
#end
For some reason at the line:
#property (retain, nonatomic) UIImage *image;
I get an error saying that "Property with 'retain (or strong)' attribute must be of object type".
My ViewController.m class is where I created three Animal objects and used the UIImage property which I created in Animal.h and set each of the Animal objects UIImage property to a certain image I have in my supporting files:
#import "ViewController.h"
#import "Animal.h"
#import "Puppy.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.whichDog = 0;
Animal *whiteDog = [[Animal alloc]init];
[whiteDog setName:#"white"];
[whiteDog setBreed:#"White Dog"];
whiteDog.image = [UIImage imageNamed:#"whitedog.jpeg"];
Animal *brownDog = [[Animal alloc] init];
[brownDog setName:#"brown"];
[brownDog setBreed:#"Brown Dog"];
brownDog.image = [UIImage imageNamed:#"browndog.jpeg"];
Animal *husky = [[Animal alloc] init];
[husky setName:#"husky"];
[husky setBreed:#"Husky Dog"];
husky.image = [UIImage imageNamed:#"husky.jpeg"];
self.myAnimals = [[NSMutableArray alloc] init];
[self.myAnimals addObject:whiteDog];
[self.myAnimals addObject:brownDog];
[self.myAnimals addObject:husky];
Puppy *pup = [[Puppy alloc]init];
[pup setName:#"coby"];
[pup setBreed:#"Portuguese Water Dog"];
pup.image = [UIImage imageNamed:#"puppy.jpeg"];
}
- (IBAction)newDogBarButton:(UIBarButtonItem *)sender{
int numOfDogs = (int)[self.myAnimals count];
int randomIndex = arc4random() % numOfDogs;
Animal *randomAnimal = [self.myAnimals objectAtIndex:randomIndex];
[UIView transitionWithView:self.view duration:1.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{
self.imageView.image = randomAnimal.image;
self.carNAme.text = randomAnimal.name;
self.extra.text = [randomAnimal breed];
} completion:^(BOOL finished) {
}];
sender.title = #"And Another";
self.whichDog = randomIndex;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
For some reason in Animal.h I keep getting that error which says "Property with 'retain (or strong)' attribute must be of object type". I am not very sure on what this retain or strong means in the properties, but can someone please explain to me what I am doing wrong in my code. Thank you so much for the help.
UIImage belongs to UIKit, so import UIKit instead of Foundation

random generator to obtain data from array not displaying

I know theres a better solution using arc4random (it's on my to-try-out-function list), but I wanted to try out using the rand() and stand(time(NULL)) function first. I've created a NSMutableArray and chuck it with 5 data. Testing out how many number it has was fine. But when I tried to use the button function to load the object it return me with object <sampleData: 0x9a2f0e0>
- (IBAction)generateNumber:(id)sender {
srand(time(NULL));
NSInteger number =rand()% ds.count ;
label.text = [NSString stringWithFormat:#"object %#", [ds objectAtIndex:number] ];
NSLog(#"%#",label.text);
}
While I feel the main cause is the method itself, I've paste the rest of the code below just incase i made any error somewhere.
ViewController.h
#import <UIKit/UIKit.h>
#import "sampleData.h"
#import "sampleDataDAO.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *onHitMePressed;
- (IBAction)generateNumber:(id)sender;
#property(nonatomic, strong) sampleDataDAO *daoDS;
#property(nonatomic, strong) NSMutableArray *ds;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize label;
#synthesize onHitMePressed;
#synthesize daoDS,ds;
- (void)viewDidLoad
{
[super viewDidLoad];
daoDS = [[sampleDataDAO alloc] init];
self.ds = daoDS.PopulateDataSource;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setLabel:nil];
[self setOnHitMePressed:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)generateNumber:(id)sender {
srand(time(NULL));
NSInteger number =rand()% ds.count ;
label.text = [NSString stringWithFormat:#"object %#", [ds objectAtIndex:number] ];
NSLog(#"%#",label.text);
}
#end
sampleData.h
#import <Foundation/Foundation.h>
#interface sampleData : NSObject
#property (strong,nonatomic) NSString * object;
#end
sampleData.m
#import "sampleData.h"
#implementation sampleData
#synthesize object;
#end
sampleDataDAO.h
#import <Foundation/Foundation.h>
#import "sampleData.h"
#interface sampleDataDAO : NSObject
#property(strong,nonatomic)NSMutableArray*someDataArray;
-(NSMutableArray *)PopulateDataSource;
#end
sampleDataDAO.m
#import "sampleDataDAO.h"
#implementation sampleDataDAO
#synthesize someDataArray;
-(NSMutableArray *)PopulateDataSource
{
someDataArray = [[NSMutableArray alloc]init];
sampleData * myData = [[sampleData alloc]init];
myData.object= #"object 1";
[someDataArray addObject:myData];
myData=nil;
myData = [[sampleData alloc] init];
myData.object= #"object 2";
[someDataArray addObject:myData];
myData=nil;
myData = [[sampleData alloc] init];
myData.object= #"object 3";
[someDataArray addObject:myData];
myData=nil;
myData = [[sampleData alloc] init];
myData.object= #"object 4";
[someDataArray addObject:myData];
myData=nil;
myData = [[sampleData alloc] init];
myData.object= #"object 5";
[someDataArray addObject:myData];
myData=nil;
return someDataArray;
}
#end
what i guess is going on is that nslog function cant print the data inside your sample data class because its not a standard class. not standard classes must implement the "description" method. What you get when you print out your class is the pointer to it, because nslog has no way of knowing how to print out the data in your class.
if what you want to print on that label/nslog is the nsstring inside your class "sampledata" you should access the property.
this can be done in the following way:
SampleData *instanceOfSampleData = (SampleData*)[ds objectAtIndex:number];
label.text = [NSString stringWithFormat:#"object %#", instanceOfSampleData.object];

How to store the values which are entered in different text fields to the Server in objective C

How to store the values which are entered in different text fields to the Server in objective C, In my project i have created the a form where it consists of different text fields where the user has to enter the values to the text field, i have kept one SAVE button, where after entering the values to the text field the user has to click the Save button.
I have to save the values entered in the text fields to the server on the click of the SAVE Button.
So how to save the data or values to the server on the click of the SAVE button.
The Following is the code i have used to create the form,
In .h File :
#import <UIKit/UIKit.h>
#import "PickerViewController.h"
#interface PopAppViewController : UIViewController < NumberPickedDelegate>{
UIPopoverController *popOverController;
UIPopoverController *popOverControllerWithPicker;
PickerViewController *pickerViewController;
IBOutlet UITextField *txtTest;
IBOutlet UITextField *txtSun;
IBOutlet UITextField *txtMon;
IBOutlet UITextField *txtTue;
IBOutlet UITextField *txtWed;
IBOutlet UITextField *txtThurs;
IBOutlet UITextField *txtFri;
IBOutlet UITextField *txtSat;
IBOutlet UITextField *txtTotal;
IBOutlet UITextField *txtTask;
IBOutlet UITextField *txtProject;
}
#property (nonatomic, retain) UIPopoverController *popOverController;
#property (nonatomic, retain) UIPopoverController *popOverControllerWithPicker;
#property (nonatomic, retain) PickerViewController *pickerViewController;
#property (nonatomic, retain) UITextField *txtTest;
#property (nonatomic, retain) UITextField *txtSun;
#property (nonatomic, retain) UITextField *txtMon;
#property (nonatomic, retain) UITextField *txtTue;
#property (nonatomic, retain) UITextField *txtWed;
#property (nonatomic, retain) UITextField *txtThurs;
#property (nonatomic, retain) UITextField *txtFri;
#property (nonatomic, retain) UITextField *txtSat;
#property (nonatomic, retain) UITextField *txtTotal;
#property (nonatomic, retain) UITextField *txtTask;
#property (nonatomic, retain) UITextField *txtProject;
-(IBAction)displayPickerPopover;
-(IBAction)exit;
-(IBAction)reset;
-(IBAction)save;
-(IBAction)total;
#end
In .m file :
#import "PopAppViewController.h"
//#import "TimeSheetDatabase.h"
#implementation PopAppViewController
#synthesize popOverController,popOverControllerWithPicker,pickerViewController,txtTest,txtSun,txtMon,txtTue,txtWed,txtThurs,txtFri,txtSat,txtTotal,txtTask,txtProject;
//-(id)initWithtxtProject:(NSString *)txtProject txtTask:(NSString *)txtTask txtSun:(int)txtSun txtMon:(int)txtMon txtTue:(int)txtTue txtWed:(int)txtWed txtThurs:(int)txtThurs txtFri:(int)txtFri txtSat:(int)txtSat txtTotal:(int)txtTotal{
//
// self=[super init];
// if(self){
// self.txtProject = txtProject;
// self.txtTask = txtTask;
// self.txtSun = txtSun;
// self.txtMon = txtMon;
// self.txtTue = txtTue;
// self.txtWed = txtWed;
// self.txtThurs = txtThurs;
// self.txtFri = txtFri;
// self.txtSat = txtSat;
// self.txtTotal = txtTotal;
//
// }
//}
-(IBAction)displayPickerPopover {
[txtTest resignFirstResponder];
CGSize sizeOfPopover = CGSizeMake(300, 422);
CGPoint positionOfPopover = CGPointMake(32, 325);
[popOverControllerWithPicker presentPopoverFromRect:CGRectMake(positionOfPopover.x, positionOfPopover.y, sizeOfPopover.width, sizeOfPopover.height)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
-(IBAction)exit{
exit(0);
}
-(IBAction)reset{
txtSun.text = #"";
txtMon.text = #"";
txtTue.text = #"";
txtWed.text = #"";
txtThurs.text = #"";
txtFri.text = #"";
txtSat.text = #"";
txtTotal.text = #"";
txtTest.text = #"";
txtTask.text = #"";
}
-(IBAction)save{
}
-(IBAction)total{
int result = [txtSun.text intValue] + [txtMon.text intValue] + [txtTue.text intValue] + [txtWed.text intValue] + [txtThurs.text intValue] + [txtFri.text intValue] + [txtSat.text intValue];
txtTotal.text = [NSString stringWithFormat:#"%d",result];
}
/*
// 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 {
}
*/
-(void)numberDidChangeTo:(NSString *)newNumber {
txtTest.text = newNumber;
}
-(void)didChangeSelection:(NSString *)newValue {
txtTest.text = newValue;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
pickerViewController = [[PickerViewController alloc] init];
pickerViewController.delegate = self;
popOverControllerWithPicker = [[UIPopoverController alloc] initWithContentViewController:pickerViewController];
popOverController.popoverContentSize = CGSizeMake(300, 216);
// NSArray *timesheetinfo = [[TimeSheetDatabase database]getAllTimeSheet];
// for(timesheetinfo *info in timesheetinfo){
//
// NSLog(#"%# - %# ",info.project,info.task);
// }
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (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 {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[popOverController release];
[popOverControllerWithPicker release];
[pickerViewController release];
[txtTest release];
[super dealloc];
}
#end
You need to compile the data into a JSON string, and then send it to the server with an NSURLRequest
-(IBAction)save
{
// build JSON string
NSDictionary *postDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.txtTest.text, #"test",
self.txtSun.text, #"sun",
self.txtSun.text, #"mon",
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONWritingPrettyPrinted error:NULL];
// perform http request (on a background thread)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:#"http://example.com/save.php" cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:postData];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
// and now go back to the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *mainQueuePool = [[NSAutoreleasePool alloc] init];
// debug: print response
NSLog(#"%#", [[NSString alloc] initWithData:responseData encoding:NSISOLatin1StringEncoding]);
// check for http error (this includes php exceptions)
if ([urlResponse statusCode] != 200) {
NSLog(#"save failed with status code != 200");
return;
}
[mainQueuePool release];
});
[pool release];
});
}
And in your php:
$rawData = file_get_contents("php://input");
$postData = json_decode($rawData);
print_r($postData);
As Objective-C supports pure C, you could use a C library like described here to connect to a MySQL Server.

variable connected to Interface builder not free memory when release it , how to fixed?

this view controller will consume memory when the view dealloc all that memory are not free
how to free that memory ??
#interface Agro01ViewController : UIViewController<UIScrollViewDelegate> {
NSInteger picIndex;
BOOL isOverrideing;
NSTimer *myTimer;
NSInteger maxPicture;
}
#property (nonatomic, retain) IBOutlet UIScrollView *myscrollView;
#property (nonatomic, retain) IBOutlet UIScrollView *myscrollView3;
#property (nonatomic, retain) IBOutlet UIView *simgView;
#property (nonatomic, retain) IBOutlet UIView *simgView3;
#property (nonatomic, retain) IBOutlet UIView *imgView;
#property (nonatomic, retain) IBOutlet UIView *imgView2;
#property (nonatomic, retain) IBOutlet UIView *imgView3;
#property (nonatomic, retain) IBOutlet UIView *imgView4;
#property (nonatomic, retain) IBOutlet UIView *imgView5;
#property (nonatomic, retain) IBOutlet UIView *imgView6;
#property (nonatomic, retain) IBOutlet UIView *imgView7;
#property (nonatomic, retain) IBOutlet UIView *imgView8;
#property (nonatomic, retain) IBOutlet UIView *Portrait;
#property (nonatomic, retain) IBOutlet UIView *Landscape;
#property (nonatomic) NSInteger maxPicture;
- (IBAction) Closebutton:(id)sender;
- (void)updatepicture;
- (IBAction) SecondViewbutton:(id)sender;
#end
#implementation Agro01ViewController
#synthesize imgView = _imgView, imgView2 = _imgView2, imgView3 = _imgView3, imgView4 = _imgView4, imgView5 = _imgView5;
#synthesize imgView6 = _imgView6, imgView7 = _imgView7, imgView8 = _imgView8, Portrait = _Portrait, Landscape = _Landscape ;
#synthesize myscrollView = _myscrollView, myscrollView3 = _myscrollView3, simgView = _simgView, simgView3 = _simgView3, maxPicture;
- (void)dealloc {
NSLog(#"dealloc");
[_Landscape release];
[_Portrait release];
[_imgView release];
[_imgView2 release];
[_imgView3 release];
[_imgView4 release];
[_imgView5 release];
[_imgView6 release];
[_imgView7 release];
[_imgView8 release];
[_myscrollView release];
[_myscrollView3 release];
[_simgView release];
[_simgView3 release];
[super dealloc];
}
- (void)viewDidUnload {
self.simgView = nil;
self.simgView3 = nil;
self.myscrollView = nil;
self.myscrollView3 = nil;
self.Landscape = nil;
self.Portrait = nil;
self.imgView = nil;
self.imgView2 = nil;
self.imgView3 = nil;
self.imgView4 = nil;
self.imgView5 = nil;
self.imgView6 = nil;
self.imgView7 = nil;
self.imgView8 = nil;
[super viewDidUnload];
}
- (void)viewDidLoad {
if (([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)) {
self.Landscape.alpha = 1;
self.Portrait.alpha = 0;
}
else {
self.Landscape.alpha = 0;
self.Portrait.alpha = 1;
}
[self.view addSubview:self.Landscape];
[self.view addSubview:self.Portrait];
self.myscrollView3.contentSize = CGSizeMake(self.simgView3.frame.size.width, self.simgView3.frame.size.height);
self.myscrollView3.maximumZoomScale = 1.0;
self.myscrollView3.minimumZoomScale = 1.0;
self.myscrollView3.clipsToBounds = YES;
self.myscrollView3.delegate = self;
self.myscrollView3.showsHorizontalScrollIndicator = NO;
self.myscrollView3.showsVerticalScrollIndicator = NO;
[self.myscrollView3 addSubview:self.simgView3];
self.myscrollView.contentSize = CGSizeMake(self.simgView.frame.size.width, self.simgView.frame.size.height);
self.myscrollView.maximumZoomScale = 1.0;
self.myscrollView.minimumZoomScale = 1.0;
self.myscrollView.clipsToBounds = YES;
self.myscrollView.delegate = self;
self.myscrollView.showsHorizontalScrollIndicator = NO;
self.myscrollView.showsVerticalScrollIndicator = NO;
[self.myscrollView addSubview:self.simgView];
isOverrideing = NO;
picIndex = 0;
[super viewDidLoad];
}
Get rid of:
- (void)viewDidUnload {
self.simgView = nil;
self.simgView3 = nil;
self.myscrollView = nil;
self.myscrollView3 = nil;
self.Landscape = nil;
self.Portrait = nil;
self.imgView = nil;
self.imgView2 = nil;
self.imgView3 = nil;
self.imgView4 = nil;
self.imgView5 = nil;
self.imgView6 = nil;
self.imgView7 = nil;
self.imgView8 = nil;
[super viewDidUnload];
}
You're clearing out the pointers to the objects you want to get rid of, before the dealloc can take place. Let the dealloc handle it.
joe

cannot follow that tutorial

i tried to follow this tutorial:
http://www.youtube.com/watch?v=L-FK1TrpUng&feature=related
around 00:39:00.
Theres a counter which recognizes every switch to the second view controller and counts it.
The number of view switches should be shown on the second view controller.
But it doesn't work, thats the code:
Test2ViewController.h
#interface Test2ViewController : UIViewController <UITextFieldDelegate> {
UINavigationController *navController;
Test21ViewController *test21View;
NSString *text;
IBOutlet UITextField *textField1;
}
#property(nonatomic, retain) UITextField *textField1;
#property (nonatomic, retain) UINavigationController *navController;
#property (copy) NSString *text;
-(IBAction)pushViewController:(id)sender;
#end
Test2ViewController.m:
#import "Test2ViewController.h"
#import "Test21ViewController.h"
#implementation Test2ViewController
#synthesize textField1, navController, text;
-(IBAction)pushViewController:(id)sender {
static int count = 1;
Test21ViewController *test21ViewController = [[Test21ViewController alloc] init];
[self.navigationController pushViewController:test21ViewController animated:YES];
test21ViewController.label = [NSString stringWithFormat:#"Pushed %d", count];
count++;
}
Test21ViewController.h:
#interface Test21ViewController : UIViewController {
UINavigationController *navController;
NSString *label;
IBOutlet UILabel *textLabel;
UITextField *tF1;
}
#property (copy) NSString *label;
#property (nonatomic, retain) UINavigationController *navController;
#property(nonatomic,retain)UITextField *tF1;
-(IBAction)feldeingabe:(id)Sender;
#end
Test21ViewController.m:
#implementation Test21ViewController
#synthesize label;
- (void)viewDidLoad {
textLabel.text = label;
}
Any ideas why it doesn't work?
Thanks in advance
Code for the second question:
static int count = 1;
Test2ViewController *test2ViewController;
test2ViewController.label = [NSString stringWithFormat:#"Pushed %d !!!!!", count];
[[self navigationController] popViewControllerAnimated:NO];
count++;
CURRENT CODE:
#import "Test21ViewController.h"
#implementation Test21ViewController
#synthesize navController, text, parentView;
-(IBAction)pushViewController2:(id)sender {
Test2ViewController *test2ViewController;
static int count = 1;
test2ViewController.parentView = self;
test2ViewController.label = [NSString stringWithFormat:#"Pushed %d !!!!!", count];
[self.navigationController pushViewController:test2ViewController animated:YES]; count++;
}
For the second problem (passing a string from child to parent);
Test2ViewController.h
#interface Test2ViewController : UIViewController <UITextFieldDelegate> {
NSString *receiverPropertyFromChild;
}
#property(nonatomic, retain) NSString *receiverPropertyFromChild;
Test2ViewController.m:
-(IBAction)pushViewController:(id)sender {
static int count = 1;
Test21ViewController *test21ViewController = [[Test21ViewController alloc] init];
test21ViewController.label = [NSString stringWithFormat:#"Pushed %d", count];
test21ViewController.parentView = self;
[self.navigationController pushViewController:test21ViewController animated:YES];
count++;
}
Test21ViewController.h:
#class Test2ViewController;
#interface Test21ViewController : UIViewController {
Test2ViewController *parentView;
}
#property (nonatomic, assign) Test2ViewController *parentView;
Test21ViewController.m
-(IBAction)goToTest2ViewController:(id)sender {
parentView.receiverPropertyFromChild = #"the text you want to pass";
[self.navigationController popViewControllerAnimated:YES];
count++;
}
can you try:
Test21ViewController *test21ViewController = [[Test21ViewController alloc] init];
test21ViewController.label = [NSString stringWithFormat:#"Pushed %d", count];
[self.navigationController pushViewController:test21ViewController animated:YES];
I swapped the last two lines. It could be that you are setting the label after viewDidLoad has already been called, thus not setting the value in the label.
That said, it is better to pass the string as an init parameter, not as a property and then set in viewDidLoad.
edit:
To set it as init parameter, you need to create a new init method to accept that parameter.
- (id) initWithString:(NSString *)labelParam {
self = [super init];
if (self) {
this.label = labelParam;
}
return self;
}