Change UILabel text - ios

I am fairly new to objective-C and XCode.
I am trying to update a text label on the screen,
I call a function loadData in viewDidLoad, which works fine. I am trying to print a string that was generated in the loadData function.
After that string is generated, I use:
self.MyLabel.text=string;
But does not update.
I am using IBOutlet as well, I think it might be related to different threads but I am not sure.
Any help is appreciated.

Have you tried updating the label after the
- (void)viewDidLoad {
// call another method before assigning to retrieve the string here.
self.MyLabel.text=string;
}
Sometimes the way the UIViewController and IB setup will need you to set in after the viewDidLoad. Try NSLog(#"%#", self.MyLabel) at wherever you try to assign to see if its not null

Your statement is correct:
self.mylabel.text = #"Test";
So make sure, that the IBOutlet is correctly connected (filled grey circle).
Check also if the string contains the correct value.

Related

Can I assign a identifier to my button so I can know which button's constraint is wrong in console

I am making a custom keyboard in xib, and the buttons' constraints are added and everything works fine. But in the console it says "Unable to simultaneously satisfy constraints.". And like "UIButton:0x7f8a38411b50.leading" and so on. But there are too many buttons here, I just don't know which button the console it talks about. Can I assign a identifier to button so I can know which button's constraint need to be fixed? By the way it's in a custom keyboard.
For the app:
You can use the View Debugger in the Xcode.
For information on the View Debugger you can go through the following link:
View Debugger Documentation by Apple
For the Keyboard Extension:
Write a category on UIButton and override the method: - (NSString *)description;
You can do something like this in your View Controller:
#implementation UIButton (ButtonDesc)
- (NSString *)description
{
return self.titleLabel.text;
}
#end
Here, I'm returning the text of the button. You can return a string that is made up of unique information like tag, background color, etc which will help you in identifying the particular button.
You might know where exactly the constraints are breaking. Put a break point there and write something like this in the console:
po 0x78f45ab0
where 0x78f45ab0 is the address of the button.
Hope this helps.
You can try with the property of UIButton which is call tag.

Getting URL of UIWebView

I'm sort of new to Xcode, so forgive me in advance for any obvious wrong things that I might write.
I'm trying to write something a bit simple using the UIWebView. I've already made it load upon the app's loading, but I can't seem to do anything else. What I want to do next is to make a button appear/disappear depending on the current URL. This is what I used to (try to) get the current URL:
NSString *currentURL = viewWeb.request.URL.absoluteString;
(I'm using this code in the ViewController.h file)
When I made the outlet (Ctrl+dragging), I named it viewWeb and I also went and labeled it viewWeb. But it doesn't seem to work and I don't know why.
Also, please don't just give me some code without explaining, because this is a bit frustrating and I want to understand it.
EDIT: Thanks, but I'm not looking for help on the disappearing button just yet (by the way, viewWeb is my UIWebView, not the button). I need help to detect an URL change to make the button disappear. Is there a webViewDidLoad or something similar? viewDidLoad isn't for this.
In the ViewController.h File, you only declare the property, like this:
#property (strong,nonatomic) NSString *currentURL;
Then you can set the value in the ViewController.m file.
maybe in the -viewDidLoad method in the ViewController.m file like this:
_currentURL = viewWeb.request.URL.absoluteString;
You can set the property of hidden or not of the button to show it selectively based on the url.
// URLString contains the URL based on which you would like to show/hide the button
if ([currentURL isEqualToString:URLString]) {
viewWeb.hidden = NO;
}
else {
viewWeb.hidden = YES;
}

Can I connect xib object to any viewController?

I know it is possible to connect an object in a XiB file (i.e button) and connect it to any viewController. I am also able to go to that viewController and programmatically set properties to that object(everything autocompletes fine, it recognizes the object properties) However, when I run the app, the button is unchanged, what gives?
Is there something I'm missing? Is there an additional step that I need to do when using a ViewController that is not the .m file related to the XIB?
Here's part of the code... I don't get any errors!
user.default_sales_rep_id = 2;
if (user.default_sales_rep_id > 0) {
buttonMask.backgroundColor = [UIColor blackColor];
}
You are most likely setting the properties on the button too early. Since you don't specify in your question where this code is located, it's hard to say but I'd guess if you put your code in awakeFromNib it would work.
- (void)awakeFromNib {
[super awakeFromNib];
//code here
}
Any changes to your view that differ from your XIB should be done in this method as it is called after the view is set up from the XIB.
Are you certain you are calling [[UIButton alloc] init] before you attempt manipulating it? I assume you have the button already as an IBOutlet, but if I recall, if you wish to make custom changes to the button, you must still do this.

How to Get the value(label) from the TextField inside the custom cell of a table View to post on te URL

I want my iOS app to pass to a URL the text the user types. The relevant code is pasted below. WHen I run the app, the text does not get assigned any value. It stays as null.
I am new to iOS programming and am probably missing something obvious here. Any help/pointers will be appreciated.
Mac OS version : 10.6.8 (64 bit)
Xcode version : 3.2.3
In the snapshop.. "I have created a Table View and placed custom cell inside each row of a table. TextField is inside the custom cell view. Post is a button next to the textField which is a done Button. If I click that button, the text we are entering in the textField should post on URL which i have specified in my code snippet."
NOTE: The text is like a comment and posting this comment that should post on particular image in that cell.
I have copied the code snippet below. I have also attached an image to explain.
-(IBAction)postAction:(id)sender
{
int index=[sender tag];
homecell *cell = [[homecell alloc] init];
UITextField *txt_c =(UITextField *)[cell.txt_comment viewWithTag:index];
NSLog(#"jj %#",txt_c);
gen_data *ut1=[[gen_data alloc]init];
gen_data *ut=[gen_data getInstance];
}
I assume that you have created that view using the interface builder, probably all you need is to connect that textfield as an IBOutlet and access the value.
IBOutlet is the key word here, search for it and you will find really fast the way to do it.
Check this link.
You should get some basics of iOS programming first.
Why are you creating a totally new custom cell that is irrelevant to your UITableView? When you create it that way, it's a totally new UITableViewCell.
You should connect it to it first, get its NSIndexPath and then operate with it and do what you want.
BTW, if you want to see the text of UITextfield you should NSLog myTextField.text
And you could easily write viewWithTag:sender.tag, no need to create an extra ivar here

setText method for UITextView only works from the second time since the method is called.

I'm having a problem with setText method for UITextView.
As I said on the title, I tried to change a UITextView text by using setText method or change the text property directly. It only works from the second time since the method is called.
My UITextView was an outlet. I even tried to change it's text directly from the owner class or create a method to call from another class, but it behaves the same.
I dont know if I'm doing sth wrong when create it as an outlet, I also tried to set it as nonatomic, strong, weak, retain but I still can't get it.
Any advice for my case? Thanks in advance! :)
EDIT:
I figured it out from David H's answer.
As my app is using tab, first tab is used for searching words, the second one for displaying the meaning, I tried to set the text before the outlet is created (as I haven't clicked the second tab yet). If I click the tab meaning first in order to let the outlet to be created, then it works perfectly.
Thanks for all the answer!
Almost for sure, the first time you try to set it the outlet is nil - not yet set. So add an assert (assert(myTextView) before you set it, or at least a NSLog message. You will surely find that the textView is nil the first time you try.

Resources