label and textview not responding to setText: - iphone

I have two properties setup properly in Objective-C:
#property (nonatomic, retain) IBOutlet UILabel *label;
#property (nonatomic, retain) IBOutlet UITextView *textView;
I have synthesised them and linked them in the NIB file however when I come to use them like:
if (row==0) {
NSLog(#"First text");
[self.label setText:#"LABEL 1"];
[self.textView setText:#"LABEL 1"];
}
else if (row==1) {
NSLog(#"Second text");
[self.label setText:#"LABEL 2"];
[self.textView setText:#"LABEL 2"];
}
The text is not changing however the NSLog is being called and not the setText: and I was wondering why...

It should be a IBOutlet for you to link them in xib.
Now that you updated your question, the problem may be you have not connected it to your viewcontroller's property. Hence the result. Add more details to your question.

Make sure that Xib and IBOutlet were correct linked.
Make sure that your label and testView are visible. You can set their background color red or other any color different with bg to check their visibility.
Make sure that the line "[self.label setText:#"LABEL 1"];" had ran. you can check the NSLog.
Make sure that the line "[self.label setText:#"LABEL 1"];" was running in main thread.

If your properties are outlets you should declare them as follows:
#property (nonatomic, retain) IBOutlet UILabel *label;
#property (nonatomic, retain) IBOutlet UITextView *textView;
The behaviour you are seeing suggests that you haven't set up the outlet correctly.

FIRST METHOD:
It should be declared as an IBOutlet and you should link it to a label and textView in XIB as Praveen S has said.
SECOND METHOD:
Other option if it is not a label in XIB then you need to allocate and initialize the label to set its text i.e. to access its properties/methods/attributes.
UILabel *label = [[UILabel alloc] init];
[label setText:#"Text"];
label.frame = CGRectMake(0,0,20,20);
Hope this helps.

Related

iPhone UITextField inputView error: Assignement to readonly property

I am having issues when I want to assign UIPicker as inputView of UITextField. It shows error " Assignement to readonly property". Please help as I think it is not related to property sythesizing.
I have added my code below:
#interface DriverWaitingDetails : UIViewController<UITextFieldDelegate,UIPickerViewDataSource, UIPickerViewDelegate>
{
IBOutlet UILabel *baseLabel;
IBOutlet UITableView *driverTableView;
IBOutlet UIPickerView *basePicker;
}
#property(nonatomic, retain) IBOutlet UIPickerView *basePicker;
#property(nonatomic,retain)IBOutlet UILabel *baseLabel;
#property(nonatomic,retain)IBOutlet UITableView *driverTableView;
#end
Implementation Code:-
-(void)viewDidLoad
{
basePicker=[[UIPickerView alloc] initWithFrame:CGRectMake(0,100,320, 500)];
self.navigationItem.title=#"Driver Table";
baseLabel.userInteractionEnabled = YES;
basePicker.delegate = self;
basePicker.dataSource = self;
[basePicker setShowsSelectionIndicator:YES];
baseLabel.inputView=nil;
[super viewDidLoad];
}
Attached Screenshot:-
You are setting the input view of UILabel.
You declared baseLabel as UILabel not UITextField.
IBOutlet UILabel *baseLabel;
You have declared
IBOutlet UILabel *baseLabel;
UILabel inherits from UIView : UIResponder : NSObject and the property is defined in UIResponder:
#property (readonly, retain) UIView *inputView
As the property inputView is read only, you cannot assign any value to it.

iphone. making UITextView but won't make it visible

#interface SomeViewController : UIViewController <UITextViewDelegate>
#property (retain, nonatomic) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) UIImage *image;
#property (nonatomic, retain) IBOutlet UITextView *textView;
I need textview(editable) in my view controller.
so I set protocol ,
and also, IB to the file's owner, too. (I don't know, is it unnecessary?)
And my .m file.... viewDidLoad..
_textView = [[UITextView alloc]init];
[_textView setFrame:CGRectMake(8, 110, 304, 82)];
[_textView setFont:[UIFont boldSystemFontOfSize:12]];
_textView.editable = YES;
//[textView setText:#"hdgffgsfgsfgds"];// if uncommented, this actually visible...
[self.view insertSubview:_textView atIndex:2];
So far, it's working. But I want these methods to run...
- (void)textViewDidChange:(UITextView *)textView
{
if([_textView.text length] == 0)
{
_textView.text = #"Foobar placeholder";
_textView.textColor = [UIColor lightGrayColor];
_textView.tag = 0;
}
}
But after build, my textview is still empty.
What did I wrong?
You need to add the delegate to your textview, try with this:
_textView.delegate = self;
On the other hand,
textViewDidChange
Will only work when you change your text, if you want to place a text in the textview, you can do:
_textView.text = #"Foobar placeholder";
_textView.textColor = [UIColor lightGrayColor];
_textView.tag = 0;
In your ViewdidLoad
When synthesizing the textView, do you do it like #synthesize textView = _textView so that you can use the underscored name? Usually the underscored one is used only in the setter/getter methods (for instance when overriding them).
Note that
The text view calls this method in response to user-initiated changes to the text. This method is not called in response to programmatically initiated changes.
This is from Apple's documentation for the method you are using. This means if you are changing the text through the code, the method will not get fired.
Are you expecting [textView setText:#"hdgffgsfgsfgds"] to in turn invoke textViewDidChange:? Calling setText will not automatically invoke the textViewDidChange:.

hide and show intermittent uilabel and uinavigation title?

Hello to all people!!
I need to made one uilabel thats shows and hide intermittently likes information message in the main view...
I need the same in the title of the navigation bar... Is this possible?
any suggestions please...
Sure.
Supposing you have:
#property (nonatomic, retain) IBOutlet UILabel * myLabel;
You can easily:
[self.myLabel setHidden:YES];
or
self.myLabel.hidden = YES;
or
[self.myLabel setHidden:NO];
or
self.myLabel.hidden = NO;
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html

Not able to push next view after clicking a uibutton which is on the top right of the view

i am having a view consisting of a tableview and a uibutton like this
when i click on button "add" a new class/xib named "Locsetting" should be open.which i am not able to do.
my code: .h file
#interface LocationViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
UITableView *table;
NSMutableArray *menuList;
LocSetting *locSetting;
IBOutlet UIButton *addbutton;
}
#property (nonatomic,retain) NSMutableArray *menuList;
#property (nonatomic,retain) IBOutlet UITableView *table;
#property (nonatomic,retain) LocSetting *locSetting;
#property (nonatomic, retain) IBOutlet UIButton *addbutton;
-(IBAction)gotoLocSetting:(id) sender;
#end
My .m :
#synthesize addbutton;
-(IBAction)gotoLocSetting:(id) sender {
NSLog(#"gotoLocSetting Entered");
locSetting = [[LocSetting alloc]
initWithNibName:#"LocSetting"
bundle:nil];
//locationViewController.menuList = [menuList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:locSetting
animated:YES];
[locSetting release];
}
what wrong i am doing?or please guide me!
Thanks
On a quick look, there might be one of the following 3 problems:
Is your IBAction linked to the button's outlet?
If not, try removing LocSetting *locSetting; and its property/synthesize. Next, change your line down there to: Locsetting *locSetting = [[LocSetting alloc] initWithNibName:#"LocSetting" bundle:nil];
If that doesn't work, try changing that to: Locsetting *locSetting = [[LocSetting alloc] initWithNibName:nil bundle:nil];
Make sure the IBAction is connected properly to the button. And I think its better to have IBOutlet UIButton *addbutton; alone and #property (nonatomic, retain) UIButton *addbutton;

Trouble setting UISwitch in FlipsideViewController

I have an iPhone utility app from the standard template, so I have MainViewController and FlipsideViewController that gets initialized and called controller. In controller's xib I have a UISwitch called pathSwitch and a UISegmentedControl called locationSelector that are outlets (and hooked up!) When I call the showInfo:(id)sender method, I do the following:
[EDIT] Adding the interface of the controller...
[EDIT 2] Updated interface to show added properties
- (IBAction)showInfo:(id)sender {
ALog(#"method begin...");
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
DLog(#">sun path visible = %#, setting flip side controller switch", sunPathIsVisible ? #"YES" : #"NO");
// deleted -> [controller.pathSwitch setOn:sunPathIsVisible];
controller.sunPathIsVisible = sunPathIsVisible; // added this
DLog(#">location mode is %d, setting flip side controller segment index to %d - 1 = %d", locationMode, locationMode, locationMode - 1);
// deleted -> controller.locationSelector.selectedSegmentIndex = locationMode - 1;
controller.delegate = self;
controller.locationMode = locationMode; // added this
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
#interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
int locationMode; // added this
UISegmentedControl *locationSelector;
BOOL sunPathIsVisible;
UISwitch *pathSwitch;
}
#property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
#property int locationMode; // added this
#property (nonatomic, retain) IBOutlet UISegmentedControl *locationSelector;
#property BOOL sunPathIsVisible; // added this
#property (nonatomic, retain) IBOutlet UISwitch *pathSwitch;;
- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
#end
// There's also the `protocol` stuff, but I left that out here...
The problem is that the controls do not accept their values and always show segment 0 and OFF. If I set their properties in controller's viewWillAppear method, they do show the correct setting.
Is controller not fully loaded when I do this or something?
You're probably right. The cleanest way would be to add some properties to the FlipsideViewController, and set those. Then let viewWillAppear set the actual switches using these properties.
This will also put the UI layout issues of FlipsideViewController where they belong, namely in FlipsideViewController and not in any other controller that may ever use it. (i.e. if you ever decide to not use a switch but some kind of button, you can change FlipsideViewController without having to look at other code)
edit
Some clarification. Try to add properties to FlipsideViewController with these lines at the relevant places:
BOOL switchState;
NSInteger locationMode;
#property (nonatomic,assign) BOOL switchState;
#property (nonatomic,assign) NSInteger locationMode;
#synthesize switchState;
#synthesize locationMode;
Then, in your current -(IBAction)showInfo:(id)sender you could say:
FlipsideViewController *controller = [[FlipsideViewController alloc]
initWithNibName:#"FlipsideView" bundle:nil];
controller.switchState = sunPathIsVisible;
controller.locationMode = locationMode;
controller.delegate = self;
// etc etc
Then, in FlipsideViewController, in viewDidLoad, put the actual handling of the switch value:
[self.pathSwitch setOn:self.sunPathIsVisible];
[self.locationSelector setSelectedSegmentIndex:self.locationMode];
This will a) solve your problem and b) separate your concerns regarding the user interface. If you would decide to change the layout of FlipsideViewController, there is no need to change any code other than that of FlipsideViewController.
There are other ways of achieving this, e.g. by letting your viewDidLoad fetch the value from its delegate, which would look like:
[self.pathSwitch setOn:[delegate pathSwitch]];
Which may be better, depending on your situation. Generally speaking I would always prefer this last approach, since it prevents synchronisation issues between your different view controllers.
I am not sure but shouldn't this be
#property (nonatomic, retain) IBOutlet UISegmentedControl *locationSelector;
#property (nonatomic, retain) IBOutlet UISwitch *pathSwitch;
instead of
#property (nonatomic, assign) IBOutlet UISegmentedControl *locationSelector;
#property (nonatomic, assign) IBOutlet UISwitch *pathSwitch;;