Transferring Data from One View to Another - objective-c

I have one string in one view controller and trying to transfer its value that it has to another string in another view controller. Here is my code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Check Mark Segue"])
{
NSLog(#"Transferring Data");
RenewDrop *controller = segue.destinationViewController;;
controller.transferData = self.renewString;
}
}
I thought this would work because it seems rather easy. However, Whenever I NSLog the transferData string in the next view it says that it is (null).
What am I doing wrong here?
EDIT:
#RenewDrop.h
#interface RenewDrop : UITableViewController <UITableViewDelegate> {
NSString *transferData;
}
#property (nonatomic, retain) NSString *transferData;
#RenewDrop.m
#implementation AutoRenewDrop
#synthesize transferData;

Okay so here is what happened: I had code in my viewDidLoad on Renew Drop that I didnt see:
//AddReminder *controller = [[AddReminder alloc] init];
//self.transferData = controller.renewString;
Once I did this everything worked just like above.
Thanks to everyone for the help!

Related

Issue adding to NSMutableArray

I have looked all over the place for anyone who has experienced this issue but have yet to find anything relevant, so I thought I'd ask it myself...
I have a custom object (HitterData) which I will use to populate cells in a UITableView, then two ViewControllers (one is hitterTableViewController, the other is a "detail" view controller labeled "AddPlayerViewController").
The problem is that I can add HitterData objects to the NSMutableArray in my Table VC, but only one, and then when I add another one using the detail view controller, the Mutable array is "reinitialized" and I can again only have one object at a time.
HitterObject:
#implementation HitterData.m
#synthesize hitterName = _hitterName;
#synthesize position = _position;
-(id)initWIthNameAndPosition:(NSString *)hitterName position:(NSString *)position {
if ((self = [super init])) {
self.hitterName = _hitterName;
self.position = _position;
}
return self;
}
HitterTableViewController.h
#import "HitterData.h"
#import "HitterDoc.h"
#import "AddPlayerViewController.h"
#interface HitterTableViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *hitters;
- (IBAction)backButton:(id)sender;
- (IBAction)addPlayerView:(id)sender;
-(void)addHitterObject:(HitterData *)hitter;
HitterTableViewController.m (only relevant to make this more readable)
#implementation HitterTableViewController
#synthesize hitters = _hitters;
- (void)viewDidLoad {
[super viewDidLoad];
self.hitters = [NSMutableArray array];
}
-(void)addHitterObject:(HitterData *)hitter {
if(_hitters != nil) {
[_hitters addObject:hitter];
} else {
_hitters = [NSMutableArray array];
[_hitters addObject:hitter];
NSLog(#"MutableArray is not valid");
}
}
AddPlayerViewController.h
#interface AddPlayerViewController : UIViewController <UITextFieldDelegate, UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UITextField *nameTextField;
#property (weak, nonatomic) IBOutlet UITextField *positionTextField;
#property (nonatomic) HitterTableViewController *hitterTable;
#property (nonatomic) NSString *hitterName;
#property (nonatomic) NSString *position;
//-(void)addNewHitterToHittersArray:(HitterData *)hitter;
- (IBAction)addPlayerToRoster:(id)sender;
AddPlayerViewController.m
#implementation AddPlayerViewController
#synthesize hitterTable;
- (void)viewDidLoad {
[super viewDidLoad];
hitterTable = [[HitterTableViewController alloc] init];
}
- (IBAction)addPlayerToRoster:(id)sender {
self.hitterName = [self.nameTextField text];
self.position = [self.positionTextField text];
HitterData *hitter = [[HitterData alloc] init];
hitter.hitterName = self.hitterName;
hitter.position = self.position;
[hitterTable addHitterObject:hitter];
ArraySingleton *arrayS = [[ArraySingleton alloc] init];
[arrayS initializeArray];
[arrayS addToHittersArray:hitter];
if (arrayS) {
NSLog(#"arrayS exists in AddPlayerVC");
} else {
NSLog(#"arrayS does not exist");
}
[self performSegueWithIdentifier:#"backToTeamTableViewController" sender:self];
}
Am I missing something here?
Guess based just on the code shown:
Every time you wish to add a player it looks like you create a new AddPlayerView/AddPlayerViewController. In turn that controller creates, in its viewDidLoad, a new HitterTableViewController - which of course has its own empty array. The code should instead be referencing the existing HitterTableViewController.
BTW: The common design pattern is MVC - model, view, controller - consider whether you are in your current situation because you've stored part of your model, the array, in your controller, and maybe both controllers should be referencing a common shared model object containing that array.
BTW: All those #synthesise statements are unneeded. In modern Obj-C synthesis is automatic and you rarely need these statements. Also it is generally recommended to not use the property backing variable directly, and certainly not when storing into the property as this breaks KVO. (There also appears to be a related typo in HitterData.m but as you don't report that as not working it is probably just a typo in your question and not code.)
HTH
AddPlayerViewController should know nothing about HitterTableViewController, return the new HitterData object with a delegate method.
- (IBAction)addPlayerToRoster:(id)sender
{
Hitter *hitter = [[Hitter alloc] init];
hitter.name = [self.nameTextField text];
hitter.position = [self.positionTextField text];
[self.delegate didAddHitter:hitter];
}
Then back in HitterTableViewController
- (void)didAddHitter:(Hitter *)hitter
{
[self.hitters addHitter:hitter];
[self dismissViewControllerAnimated:YES completion:nil];
}

Variable not being modified Xcode

I'm really lost as to what is happening here.
I am basically trying to pass a variable between two classes but it doesn't even get to that point for one of these variables.
I have two Strings and two buttons declared.
#property (nonatomic, strong) NSString *qm;
#property (nonatomic, strong) NSString *m;
#property (nonatomic, strong) IBOutlet UIButton *mBtn;
#property (nonatomic, strong) IBOutlet UIButton *qmBtn;
I synthesise the strings..
#synthesize qm;
#synthesize m;
Initialise the strings...
- (void)viewDidAppear:(BOOL)animated{
qm = #"0";
m = #"0";
}
And these methods reassign the new variable based on the selected button on the view (I've definitely made sure to link the buttons to their corresponding variables and methods from the view)
- (IBAction)mPressed:(id)sender {
m = #"1";
}
- (IBAction)qmPressed:(id)sender {
qm = #"1";
NSLog(#"This value%#", qm);
}
The NSLog prints the value 1, which is what I want
When the program enters the segue part. Things don't work as planned...
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"Here:%#", qm);
if([m isEqualToString: #"1"]){
QMMPOpViewController *mpOp = [segue destinationViewController];
NSLog(#"test 1:%#",self.qm);
mpOp.qm = self.qm;
mpOp.selectedQuiz = self.selectedQuiz2;
NSLog(#"This value2%#", qm);
NSLog(#"This value3%#", m);
}
else if([qm isEqualToString: #"1"]){
QMMPOpViewController *mpOp = [segue destinationViewController];
NSLog(#"test 2:%#",self.qm);
mpOp.qm = self.qm;
mpOp.selectedQuiz = self.selectedQuiz2;
NSLog(#"This value2%#", qm);
NSLog(#"This value3%#", m);
}
else{
QMSPController *tableVC = [segue destinationViewController];
tableVC.selectedQuiz = self.selectedQuiz2;
}
}
I have changed the value of qm; the NSLog outputs the value 0 instead of the new variable, 1. So therefore ([qm isEqualToString: #"1"]) is never called.
This is so simple yet it still doesn't work despite my attempts to debug the issue. The m value executes fine. Though identical in their means of operation. For some reason the other one does not.
Maybe I'm missing something blatantly obvious or basic. Or if someone could let me know what could possibly be going on here that would be greatly appreciated.

property not found on object of type

this seems just bizarre as i am not able to resolve it and stuck over it. I am using storyboard to navigate between tableview and a detailview. It was working fine when i was passing a single (NewsRecord) object from my tableview class(TopStoriesViewController) to my detail class(DetailNewsViewController). But now i need to pass an array of (NewsRecord) objects when moving to the detail class instead of a single (NewsRecord) object. But when i create a NSArray * in my detail class and try to access it in my tableview class in prepareForSegue method using the object of detail class it gives the following error---property 'items' not found on object of type 'DetailNewsViewController *' at compile time. items is a NSArray object which get its contents from the 'entries' which is also an NSArray in TopStoriesViewController class.
My question is why am i able to access getNewsDetails of DetailNewsViewController in TopStoriesViewController and not items.
My classes are as follows -
TopStoriesViewController.m
#import "DetailNewsViewController.h"
some code here....
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowDetailedNews"]) {
DetailNewsViewController *detailNewsVC = [segue destinationViewController];
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
NSInteger indexForNewsSelectedFromTBV = [[self.tableView indexPathForSelectedRow] row];
[detailNewsVC setGetNewsDetails:[entries objectAtIndex:indexForNewsSelectedFromTBV]]; //This is working fine...
detailNewsVC.items=entries; //Error is occurring here...
}
}
DetailNewsViewController.h
#import "NewsRecord.h"
#interface DetailNewsViewController : UIViewController {
NewsRecord *getNewsDetails;
some other declarations...
NSArray *items;
}
#property(nonatomic,retain) NewsRecord *getNewsDetails;
#property(nonatomic,retain) NSArray *items;
#end
DetailNewsViewController.m
#import "DetailNewsViewController.h"
#synthesize getNewsDetails,items;
NewsRecord.h
#interface NewsRecord : NSObject {
NSString *newsTitle;
NSString *newsDescription;
}
#property(nonatomic,retain) NSString *newsTitle;
#property(nonatomic,retain) NSString *newsDescription;
#end
You should try to explicitely use the setter for items:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowDetailedNews"])
{
DetailNewsViewController *detailNewsVC = [segue destinationViewController];
...
[detailNewsVC setItems:entries];
}
}

UITableView and delegates

I have an iPad app in progess but I'm having difficulty catching the selection of a row in my table view. I know this is because I haven't defined my delegate properly yet but, after 2 hours on the net, it still isn't making much sense.
What I'm trying to do is pass the selected table row item to a new view that displays info based on the selection - pretty standard.
I set up the tableViewController sub class using the option to create it as a UITableViewController subclass which, unless I am wrong, incorporates the delegates (UITableViewDelegate and UITableViewDataSource) automatically.
In the didSelectRowsAtIndex method I'm trying to create a DetailViewController. I've tried with a nib file and creating one purely in code but the class is never created. I'm missing a step I'm sure of it but I can't find what it is. At some point shouldn't I be defining what function I want to access with the selected row? But where? How?
In what I considered was my best attempt, I created the DetailViewController, set a string variable in the detailViewController to the selected row, and then tried to add the detailViewController view to display. I figured I could then use the viewDidload to call the next function but the view never got displayed on screen.
Some basic guidence would be nice. Or a decent tutorial would be nice. No calls to read the relevant docs please, I've been over it and right now I need a example to pull things together.
Thanks,
Steve
I think you are missing this line in your didSelectRowAtIndexPath: method
[self presentModalViewController:controller animated:YES];
where controller is an object of class DetailViewController
Yeah, maybe paste the code snippet will be easier to figure out what's going on here. And are those delegate(didSelectRowAtIndexPath:) methods being called correctly?
Try this,
this goes in didSelectRowAtIndexPath
MoreInfoTable *moreInfoView = [[MoreInfoTable alloc] initWithStyle:UITableViewStyleGrouped];
//in the MoreInfoTable, make properties like titles etc.
[self.navigationController pushViewController:moreInfoView animated:YES];
[moreInfoView release];
}
here's an example of an MoreInfoTable.h
#interface MoreInfoTable : UITableViewController {
NSMutableArray *moreInfo;
NSURL *getDirections;
NSURL *getWebsite;
NSMutableString *getPhoneNumber;
NSString *address;
NSString *footer;
float lat, lon;
}
-(void)goToWebsite;
-(void)goToMaps;
-(IBAction)addToFavorites:(id)sender;
-(void) callNumber;
#property (nonatomic,retain) NSURL *getDirections;
#property (nonatomic,retain) NSURL *getWebsite;
#property (nonatomic,retain) NSMutableString *getPhoneNumber;
#property (nonatomic,retain) NSString *footer;
#property (nonatomic,retain) NSString *address;
#property (readwrite) float lat;
#property (readwrite) float lon;
#end
now back in the other file in which you declare the table, you can say
MoreInfoTable *moreInfoView = [[MoreInfoTable alloc] initWithStyle:UITableViewStyleGrouped];
//in the MoreInfoTable, make properties like titles etc.
moreInfoView.title = #"TITLE!";
//etc.
[self.navigationController pushViewController:moreInfoView animated:YES];
[moreInfoView release]; //

Objective-C NSMutableArray Count Causes EXC_BAD_ACCESS

I've been stuck on this for days and each time I come back to it I keep making my code more and more confusing to myself, lol. Here's what I'm trying to do. I have table list of charges, I tap on one and brings up a model view with charge details. Now when the model is presented a object is created to fetch a XML list of users and parses it and returns a NSMutableArray via a custom delegate. I then have a button that presents a picker popover, when the popover view is called the user array is used in an initWithArray call to the popover view. I know the data in the array is right, but when [pickerUsers count] is called I get an EXC_BAD_ACCESS. I assume it's a memory/ownership issue but nothing seems to help. Any help would be appreciated.
Relevant code snippets:
Charge Popover (Charge details model view):
#interface ChargePopoverViewController .....
NSMutableArray *pickerUserList;
#property (nonatomic, retain) NSMutableArray *pickerUserList;
#implementation ChargePopoverViewController
#synthesize whoOwesPickerButton, pickerUserList;
- (void)viewDidLoad {
JEHWebAPIPickerUsers *fetcher = [[JEHWebAPIPickerUsers alloc] init];
fetcher.delegate = self;
[fetcher fetchUsers];
}
-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
[pickerUserList release];
pickerUserList = theData;
}
- (void) pickWhoPaid: (id) sender {
UserPickerViewController* content = [[UserPickerViewController alloc] initWithArray:pickerUserList];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:content];
[popover presentPopoverFromRect:whoPaidPickerButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
content.delegate = self;
}
User Picker View Controller
#interface UserPickerViewController .....
NSMutableArray *pickerUsers;
#property(nonatomic, retain) NSMutableArray *pickerUsers;
#implementation UserPickerViewController
#synthesize pickerUsers;
-(UserPickerViewController*) initWithArray:(NSMutableArray *)theUsers {
self = [super init];
if ( self ) {
self.pickerUsers = theUsers;
}
return self;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
// Dies Here EXC_BAD_ACCESS, but NSLog(#"The content of array is%#",pickerUsers); shows correct array data
return [pickerUsers count];
}
I can provide additional code if it might help. Thanks in advance.
You declare the ivar holding the array as this...
#property (nonatomic, retain) NSMutableArray *pickerUserList;
But then you have a method implemented like this:
-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
[pickerUserList release];
pickerUserList = theData;
}
You aren't retaining theData and you aren't calling the synthesized setter. If you did Build and Analyze, it should catch this problem and tell you about it. If not, file a bug.