UILabel text property iOS - ios

I would like to modify the text property of a UILabel (nameLbl) but don't know what i am wrong.
I have 2 Views, the first is a ViewController, the second is ProfileViewController. The ProfileViewController fill some field of user profile this data are passed to ViewController and showed in UIlabel.
The issue is I can't show the datas in the ViewController.
What am I get wrong?
---ViewController.h---
#interface ViewController : UIViewController
{
IBOutlet ViewController *profile;
IBOutlet UILabel *nameLbl, *celLbl, *cfLbl;
IBOutlet UITextField *nameTF;
}
#property (nonatomic, retain) IBOutlet ViewController *profile;
#property (retain, nonatomic) IBOutlet UILabel *nameLbl, *celLbl, *cfLbl;
#property (nonatomic, retain) IBOutlet UITextField *nameTF;
-(void) setUser: (NSString *) name:(NSString *) cel:(NSString *) cf;
#end
---ViewController.m---
#synthesize nameLbl, celLbl, cfLbl;
-(void) setUser:(NSString *)name:(NSString *)cel:(NSString *)cf
{
nameLbl = [[UILabel alloc] init];
[nameLbl setText:name];
}
this is the connection inspector
https://drive.google.com/file/d/0Bz7WcQmZNuFLMWt3QUo1Tk5XUW8/edit?usp=sharing

Remove
nameLbl = [[UILabel alloc] init];
from your code it seems that the nameLbl UILabel has already been initialized from nib, and you are creating a new memory reference, so its not working.

Related

After converting to ARC I am getting "message sent to deallocated instance"

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.

Cannot assign to view controller's property after instantiating from storyboard

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";

UIButton won't hide/unhide

I set UIButton myButton to hidden on viewDidLoad
In a splitviewcontroller I have it so that when I click on a tablecell, certain items unhide which works great for my textviews but my button is giving me this:
ERROR: member reference base type void is not a structure or union
Here is some snippet of code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
detailViewController.myButton.hidden= NO;
detailViewController.textView1.hidden= NO;
detailViewController.textView2.hidden= NO;
}
in the .h file there is
#property (strong, nonatomic) DetailViewController *detailViewController;
in DetailViewController is where the button and textviews are declared as
#interface DetailViewController : UIViewController <UISplitViewControllerDelegate>{
IBOutlet UIButton *myButton;
IBOutlet UITextView *textView1;
IBOutlet UITextView *textView2;
}
#property (strong, nonatomic) IBOutlet UITextView *textView1;
#property (strong, nonatomic) IBOutlet UITextView *textView2;
#property (strong, nonatomic) UIButton *myButton;
-(IBAction)myButtonPressed;
#end
IBAction myButtonPressed in the DetailViewController.m is
-(IBAction)myButtonPressed{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
RootViewController *RVC = [storyboard instantiateViewControllerWithIdentifier:#"Root"];
[UIApplication sharedApplication].delegate.window.RootViewController = RVC;
}
Anyone have any thoughts on why the button won't behave like the other two will? Is it because I gave it an IBAction??
You forget to Put IBOutlet at the first of button creation it should be
#property (strong, nonatomic) IBOutlet UIButton *myButton; OR
#property (nonatomic, weak) IBOutlet UIButton *myButton;
And give proper connection to file's owner.
Button should be a IBOutlet and please correct it and update XIB references and you should be good.
#property (nonatomic, weak) IBOutlet UIButton * myButton;
link this to UIButton is XIB if you are using one.

Issue Passing Values From One View Controller To Another

I am trying to take the text that is typed into UITextFields in one view, and pass those values into a different View Controller when a button is pressed. In the first view controller (CreateViewController) I have:
#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
#class ThePreviewViewController;
#interface CreateViewController : UIViewController <UIAlertViewDelegate, MFMailComposeViewControllerDelegate> {
UIDatePicker *datePicker;
UIDatePicker *datePicker2;
ThePreviewViewController *_thePreview;
}
#property (nonatomic, retain) IBOutlet UITextField *toName;
#property (nonatomic, retain) IBOutlet UITextField *fromName;
#property (nonatomic, retain) IBOutlet UITextField *issue;
#property (nonatomic, retain) ThePreviewViewController *thePreview;
#property (nonatomic, retain) IBOutlet UITextField *expire;
#property (nonatomic, retain) IBOutlet UITextField *loveMessage;
#property (nonatomic, retain) IBOutlet UITextField *gift;
-(IBAction)build;
#end
The implementation file is:
#import "ThePreviewViewController.h"
#implementation CreateViewController
#synthesize toName, fromName, gift, loveMessage, issue, expire, datePicker, datePicker2;
#synthesize thePreview = _thePreview;
-(IBAction)preview {
if (_thePreview == nil) {
self.thePreview = [[ThePreviewViewController alloc] initWithNibName:#"ThePreviewViewController" bundle:[NSBundle mainBundle]] ;
}
_thePreview.issue.text = issue.text;
_thePreview.gift.text = gift.text;
_thePreview.expire.text = expire.text;
[self.navigationController pushViewController:_thePreview animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
However, after I connect all the outlets, the labels in ThePreviewViewController stay at null. Thoughts?
It looks like this is due to an issue which has been discussed here before: when you instantiate your ThePreviewViewController its UIKit instances are not yet available to have their properties set.
The solution is to create and set NSString properties on your ThePreviewViewController (or more compactly use an NSDictionary) which then use, in its ViewWillAppear event handler to set the properties of its UIKit elements.
Instead of setting UITextField text in ThePreview VC, you should set a NSString property. This is because the UITextField might not have been loaded yet and _thePreview.issue.text = issue.text, will have no effect, since _thePreview.issue has still not been created. So create a property (eg. NSString *issueString) in your ThePreview VC and then set that property as _thePreview.issueString = issue.text in your CreateViewController, and then finally in your ThePreview VC set your textField text in your viewDidLoad method using self.issue.text = self.issueString;
Hope this helps.

IB Connections in Xcode crashing

I was working on my iOS app last night when I went to test it and it crashed on startup. I wasn't even working on the nib that is causing the crash. Anyways, here is the error code:
2/29/12 10:32:05.291 AM Safe Flight: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0xdd496f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Airport1.'
*** First throw call stack:
(0x2873052 0x33add0a 0x2872f11 0x1ae2032 0x1a53f7b 0x1a53eeb 0x1a6ed60 0x136191a 0x2874e1a 0x27de821 0x136046e 0x1362010 0x114214a 0x1142461 0x11417c0 0x1150743 0x11511f8 0x1144aa9 0x37b7fa9 0x28471c5 0x27ac022 0x27aa90a 0x27a9db4 0x27a9ccb 0x11412a7 0x1142a9b 0x3710 0x2f35)
Airport1 is just a label with an IBOutlet correctly connected to it. Also, if I delete the connection the error just is with the next item that has an connection.
Also, my FileOwners class it correctly set to the appropriate viewController.
// MasterViewController.h
#import <UIKit/UIKit.h>
#import "MobclixAds.h"
#class DataViewController;
#class EditViewController;
#class SearchViewController;
#interface MasterViewController : UIViewController{
UILabel *Airport1;
UILabel *Airport2;
UILabel *Airport3;
UILabel *Airport4;
UILabel *Airport5;
UILabel *Airport6;
UILabel *Airport7;
UILabel *Airport8;
UILabel *Airport9;
UIButton *Airport1B;
UIButton *Airport2B;
UIButton *Airport3B;
UIButton *Airport4B;
UIButton *Airport5B;
UIButton *Airport6B;
UIButton *Airport7B;
UIButton *Airport8B;
UIButton *Airport9B;
MobclixAdView* adView;
}
#property (strong, nonatomic) DataViewController *dataViewController;
#property (strong, nonatomic) EditViewController *editViewController;
#property (strong, nonatomic) SearchViewController *searchViewController;
#property (nonatomic, retain) IBOutlet UILabel *Airport1;
#property (nonatomic, retain) IBOutlet UILabel *Airport2;
#property (nonatomic, retain) IBOutlet UILabel *Airport3;
#property (nonatomic, retain) IBOutlet UILabel *Airport4;
#property (nonatomic, retain) IBOutlet UILabel *Airport5;
#property (nonatomic, retain) IBOutlet UILabel *Airport6;
#property (nonatomic, retain) IBOutlet UILabel *Airport7;
#property (nonatomic, retain) IBOutlet UILabel *Airport8;
#property (nonatomic, retain) IBOutlet UILabel *Airport9;
#property (nonatomic, retain) IBOutlet UIButton *Airport1B;
#property (nonatomic, retain) IBOutlet UIButton *Airport2B;
#property (nonatomic, retain) IBOutlet UIButton *Airport3B;
#property (nonatomic, retain) IBOutlet UIButton *Airport4B;
#property (nonatomic, retain) IBOutlet UIButton *Airport5B;
#property (nonatomic, retain) IBOutlet UIButton *Airport6B;
#property (nonatomic, retain) IBOutlet UIButton *Airport7B;
#property (nonatomic, retain) IBOutlet UIButton *Airport8B;
#property (nonatomic, retain) IBOutlet UIButton *Airport9B;
#property(nonatomic,retain) IBOutlet MobclixAdView* adView;
#end
and
// MasterViewController.m
#import "MasterViewController.h"
#import "DataViewController.h"
#import "EditViewController.h"
#import "SearchViewController.h"
#implementation MasterViewController
#synthesize dataViewController = _dataViewController;
#synthesize editViewController = _editViewController;
#synthesize searchViewController = _searchViewController;
#synthesize Airport1, Airport2, Airport3, Airport4, Airport5, Airport6, Airport7, Airport8, Airport9;
#synthesize Airport1B, Airport2B, Airport3B, Airport4B, Airport5B, Airport6B, Airport7B, Airport8B, Airport9B;
#synthesize adView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Safe Flight";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
}
return self;
}
So is my problem with my code? or with Xcode itself?? I have restarted Xcode and the simulator and even did a "clean" build (it sounded like it would help...)
Thanks,
Andrew
EDIT This is my AppDelegate and I'm setting the correct xib to the window
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Mobclix start];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
PROBLEM SOLVED!!!
I can't answer my own question because of some stupid spam filter... and I can post a picture of the problem... but its fixed!
but here is a link to the problem click here
Found the Problem!! I feel kinda stupid now ;) but anyways, here is a screenshot of the problem!
The reason causing to crash could be the connection mistakes within xib to header.
Airport1 could be connected to another class than MasterViewController.
If your label connected to File's Owner - it should be MasterViewController.
If you've created another UIViewController within xib and connected Airport1 label to it, then this controller also should be connected to MasterViewController (Identity Inspector "Class" field.)
Updated. Added Screenshots:
When you initialize the MasterViewController (wherever you are launching it from), make sure you are initializing it with the appropriate nib file
Ex.
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"YourNibName" bundle:nil];

Resources