I've got 2 view controller,
i want to pass data between them, in the first view controller i use this method to switch to other view controller and passing data :
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
NSString *userName = [user username];
NSString *userId = [user id];
dashBoardViewController *dashBoardViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"dashBoardViewController"];
dashBoardViewController.first_name = userName;
dashBoardViewController.id = userId;
[self presentModalViewController:dashBoardViewController animated:YES];
}
having this on view controller.h
#interface ViewController : UIViewController <FBLoginViewDelegate>
#property (weak, nonatomic) IBOutlet FBLoginView *loginButton;
#property (weak, nonatomic) IBOutlet UIButton *showEvent;
#property (weak, nonatomic) IBOutlet UIButton *goQrcode;
#property (retain, nonatomic) NSString *id;
#property (retain, nonatomic) NSString *first_name;
in the second view controller i have this method to read the variable and switch to another view controller:
- (IBAction)gotoEvents:(id)sender {
events *events;
events.first_name = _first_name;
events.userid = _id;
[self.storyboard instantiateViewControllerWithIdentifier:#"events"];
[self presentModalViewController:events animated:YES];
}
and this on my header file:
#property (nonatomic, retain) NSString *first_name;
#property (nonatomic, retain) NSString *id;
- (IBAction)gotoEvents:(id)sender;
it says me
property 'first_name' not found on object of type 'events'
Try this,
events *events = [self.storyboard instantiateViewControllerWithIdentifier:#"events"];
events.first_name = _first_name; //class name Should Start With capital letter and should use camel case for naming.
events should be named as Events, as it the class name
Related
The goal for this project is to let user enter two separate string and be able to concat together. In the console, you can see all concat history users entered.
For example, 1st time, user entered "app" and "le", and clicked button "combine" then console stored "apple".
Then, user entered "123" and "45", then in the console it should show that user entered "apple" and "12345".
My problem is now, I couldn't store any user input in my mutable array.
I appreciate it if someone can help!
Here's my code:
import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *myString1;
#property (weak, nonatomic) IBOutlet UITextField *myString2;
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
#property (weak, nonatomic) IBOutlet UILabel *combinedString;
#property (nonatomic, strong) NSMutableArray *arrayhere;
#property int arrayCount;
- (IBAction)buttonPressed:(id)sender {
NSString *userInput1 = self.myString1.text;
NSString *userInput2 = self.myString2.text;
NSString *result = [NSString stringWithFormat: #"%#%#", userInput1, userInput2];
self.combinedString.text = result;
[self.arrayhere insertObject:result atIndex:self.arrayCount];
self.arrayCount++;
NSLog(#"list of portmanteaus is %# ", self.arrayhere);
}
In you viewDidLoad: you need to add:
self.arrayhere = [[NSMutableArray alloc] init];
Then in your buttonPressed action, you need to replace:
[self.arrayhere insertObject:result atIndex:self.arrayCount];
with:
[self.arrayhere addObject:result];
This will automatically add that object to the end of your array, no need to have a property for the count of the array, as #rmaddy stated it is already a property of NSMutableArray.
Therefore, you can also remove this line:
self.arrayCount++;
You should try this,
import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *myString1;
#property (weak, nonatomic) IBOutlet UITextField *myString2;
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
#property (weak, nonatomic) IBOutlet UILabel *combinedString;
#property (nonatomic, strong) NSMutableArray *arrayhere;
#property int arrayCount;
- ( void )viewDidLoad {
[super viewDidLoad];
arrayhere = [NSMutableArray new];
}
- (IBAction)buttonPressed:(id)sender {
NSString *userInput1 = self.myString1.text;
NSString *userInput2 = self.myString2.text;
NSString *result = [NSString stringWithFormat: #"%#%#", userInput1, userInput2];
[arrayhere addObject:result];
NSLog(#"list of portmanteaus is %# ", self.arrayhere);
}
I am creatinng an instance of a View like this...
- (IBAction)findPatientTapped:(id)sender {
RequestDialogViewController *findPatientViewController = [[RequestDialogViewController alloc] initWithNibName:#"RequestDialogViewController" bundle:nil];
findPatientViewController.delegate = self;
findPatientViewController.autoCorrectOff = YES;
findPatientViewController.defaultResponse = #"";
findPatientViewController.keyboardType = #"ASCII";
findPatientViewController.returnKeyType = #"Go";
findPatientViewController.tag = findRequestTag;
findPatientViewController.editSet = YES;
findPatientViewController.titleText = #"Find Patient";
findPatientViewController.messageText =#"Enter all or a portion of the patient's name...";
findPatientViewController.messageText2 = #"Last Name...";
findPatientViewController.messageText3 = #"First Name...";
findPatientViewController.showResponse2 = YES;
findPatientViewController.showNavBarSaveButton = NO;
findPatientViewController.infoTextFile = #"";
findPatientViewController.view.frame = CGRectMake(280, 230, 480, 300);
[self.view addSubview:findPatientViewController.view];
}
The view contains 2 UITextField fields for entry by the user. This all worked fine before running the convert to ARC tool. After the conversion the view crashes when trying to enter a value in either of the 2 fields with...
-[RequestDialogViewController respondsToSelector:]: message sent to deallocated instance
Here is the .h file of the UIViewController after running the conversion to ARC tool...
#import <UIKit/UIKit.h>
#protocol RequestDialogViewControllerDelegate;
#interface RequestDialogViewController : UIViewController <UITextFieldDelegate>{
id<RequestDialogViewControllerDelegate> __weak delegate;
IBOutlet UINavigationBar *navBar;
IBOutlet UITextField *response;
IBOutlet UITextField *response2;
IBOutlet UILabel *message;
IBOutlet UILabel *message2;
IBOutlet UILabel *message3;
IBOutlet UIButton *saveButton;
NSTimer *selectAllTimer;
NSString *defaultResponse;
NSString *titleText, *infoTextFile;
NSString *messageText, *messageText2, *messageText3;
NSString *placeHolderText;
NSString *keyboardType, *returnKeyType;
BOOL editSet, showSaveButton,showNavBarSaveButton, showResponse2, autoCorrectOff;
int tag;
}
#property (weak) id<RequestDialogViewControllerDelegate> delegate;
#property (nonatomic, strong)IBOutlet UITextField *response;
#property (nonatomic, strong) IBOutlet UITextField *response2;
#property (nonatomic, strong)IBOutlet UILabel *message;
#property (nonatomic, strong)IBOutlet UILabel *message2;
#property (nonatomic, strong) IBOutlet UILabel *message3;
#property (nonatomic, strong)IBOutlet UIButton *saveButton;
#property (nonatomic, strong)NSString *defaultResponse;
#property (nonatomic, strong)NSString *titleText, *infoTextFile;
#property (nonatomic, strong)NSString *messageText, *messageText2, *messageText3;
#property (nonatomic, strong)NSString *placeHolderText;
#property (nonatomic, strong)NSString *keyboardType, *returnKeyType;
#property (nonatomic, strong)IBOutlet UINavigationBar *navBar;
#property (readwrite)BOOL editSet, showSaveButton, showNavBarSaveButton, showResponse2, autoCorrectOff;
#property (readwrite)int tag;
#property (nonatomic, strong) NSTimer *selectAllTimer;
- (IBAction)saveButtonPressed:(id)sender;
- (void)selectAll;
- (IBAction)editingDidEnd:(id)sender;
#end
#protocol RequestDialogViewControllerDelegate <NSObject>
#optional
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)reqResponse response2:(NSString*)reqResponse2;
#end
Here is the pertenant references in the .h file of the class creating the instance of the RequestDialog view...
#import "RequestDialogViewController.h"
#interface OrthoViewController : UIViewController <RequestDialogViewControllerDelegate>{
}
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)reqResponse response2:(NSString*)reqResponse2;
#end
What do I need to do to make this work under ARC? I am thinking it might have something to do with how the protocol is formed.
Thanks,
John
**** Thanks to Dan I resolved this by making findPatientViewController a property of the calling class...
RequestDialogViewController *findPatientViewController;
#implementation OrthoViewController
- (IBAction)findPatientTapped:(id)sender {
findPatientViewController = [[RequestDialogViewController alloc] initWithNibName:#"RequestDialogViewController" bundle:nil];
//set all the properties and add the subview
}
#end
Your findPatientViewController has nothing retaining it, so it gets deallocated at the end of the method where you create it. Then, when something in it's view tries to call a delegate method on it you get that crash.
If findPatientTapped is a method in a view controller then you should add the findPatientViewController as a child view controller. If it's in a view, then you need to least store the findPatientViewController in a property so it doesn't get deallocated while you are still using it.
Your code didn't really work properly before ARC, you just had a memory leak.
I'm instantiating a view controller from the storyboard with this code:
CalloutViewController *calloutVC = (CalloutViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"CalloutViewController"];
and trying to assign to it's property nameLabel, declared here in its header:
#interface CalloutViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *nameLabel;
...
#end
using
calloutVC.nameLabel.text = #"test";
However, after the assignment, the property is still nil.
It's because the IBOutlet is not loaded yet. You need to create a NSString property and assign your value to it. And in your CalloutViewController's viewDidLoad assign the value to your IBOutlet.
Like:
#interface CalloutViewController : UIViewController
#property (nonatomic, weak) IBOutlet UILabel *nameLabel;
#property (nonatomic, copy) NSString *nameValue;
...
#end
And in your viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
self.nameLabel.text = self.nameValue;
...
}
And assign the property like:
calloutVC.nameValue = #"test";
I am trying to use Delegates to communicate thru view controllers. Here is my code:
#protocol ViewControllerDelegate;
#interface ViewController : UIViewController <UITextFieldDelegate>{
IBOutlet UIDatePicker *dateTimePicker;
}
#property (nonatomic, strong) IBOutlet UITextField *className;
#property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
#property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
#property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
#property (nonatomic, strong) IBOutlet UILabel *notificationOnOffLabel;
#property (nonatomic, assign) id <ViewControllerDelegate> delegate;
#property (nonatomic,strong)classObject *classObject;
-(IBAction) addButton:(id)sender;
#end
#protocol ViewControllerDelegate <NSObject>
-(void)assignmentSaved:(classObject *)classObj;
In action:
[self.delegate assignmentSaved:self.classObject];
In other view controller:
-(ViewController *)vc
{
if (!_vc) {
_vc = [[ViewController alloc]init];
}
return _vc;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.vc.delegate = self;
}
-(void)assignmentSaved:(classObject *)classObj
{
NSLog(#"%#",classObj.description);
classObj2 = classObj;
[self.tableView reloadData];
}
vc is a property of View Controller. When I NSLog the delegate, the delegate ends up to be nil. Also I tried #mialkan answer yet it still does not work. If you need any more info just ask.
I made some changes in your code, and dont forget to #synthesize delegate; and dont for get to setDelegate:self where you create ViewController.
#protocol ViewControllerDelegate;
#interface ViewController : UIViewController <UITextFieldDelegate>{
IBOutlet UIDatePicker *dateTimePicker;
id <ViewControllerDelegate> delegate;
}
#property (nonatomic, strong) IBOutlet UITextField *className;
#property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
#property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
#property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
#property (nonatomic, strong) IBOutlet UILabel *notificationOnOffLabel;
#property (nonatomic, assign) id <ViewControllerDelegate> delegate;
#property (nonatomic,strong)classObject *classObject;
-(IBAction) addButton:(id)sender;
#end
#protocol ViewControllerDelegate
-(void)assignmentSaved:(classObject *)classObj;
#end
In the first view did you subscribe to your delegate < ViewControllerDelegate > ?
MyOtherViewController : UIViewController <ViewControllerDelegate>
and you can only do that :
- (void)viewDidLoad
{
[super viewDidLoad];
if (!_vc)
_vc = [[ViewController alloc]init];
self.vc.delegate = self;
}
I've got 2 views. The firstView and the secondView. The firstView needs the favourites array from the secondView, so I try to call the getFavourites method defined in the protocol. This returns null however, which seems strange to me because everything makes sense.
Here is the firstViewController.h:
#import <UIKit/UIKit.h>
#import "Drink.h"
#protocol firstViewControllerDelegate;
#interface FirstViewController : UIViewController
{
id <firstViewControllerDelegate> delegate;
NSMutableArray *labels;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *button4;
}
- (IBAction) buttonClick: (id) sender;
#property (nonatomic, assign) id <firstViewControllerDelegate> delegate;
#property (nonatomic, retain) IBOutlet UIButton *button1;
#property (nonatomic, retain) IBOutlet UIButton *button2;
#property (nonatomic, retain) IBOutlet UIButton *button3;
#property (nonatomic, retain) IBOutlet UIButton *button4;
#end
#protocol firstViewControllerDelegate
- (NSMutableArray *) getFavourites;
- (void) setFavourites: NSMutableArray;
#end
firstViewController.m
#synthesize delegate;
.......
- (void) viewWillAppear:(BOOL)animated
{
NSMutableArray *favourites = [delegate getFavourites]; // favourites is empty after this line
[button1 setTitle:[[favourites objectAtIndex:1] name] forState:UIControlStateNormal];
NSLog(#"VIEW APPEARED. BUTTON TITLE IS: %#", button1.currentTitle);
}
SecondViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#interface SecondViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, firstViewControllerDelegate>
{
NSMutableArray *favourites;
NSMutableArray *drinks;
}
#property (nonatomic, retain) NSMutableArray *drinks;
#property (nonatomic, retain, getter = getFavourites) NSMutableArray *favourites;
- (void) addDrink: (NSString *) name;
#end
Does anybody have any idea why this isn't working?
Are you sure you're setting the delegate property of FirstViewController? i.e.:
FirstViewController *firstController = // Instantiate the controller;
firstController.delegate = secondController;
[self.navigationController pushViewController:firstController animated:YES];
secondController should exist before instantiation, otherwise you're setting a nil value