Unable to set the UILabel of a view Controller from a different view Controller - objective-c

I am trying to set the text label of a second view controller from the current view controller using the following code:
NSString *loadingString = [NSString stringWithFormat:#"Loading data from Instahotness....."];
self.loadingPage = [[LoadingPageViewController alloc]init];
self.loadingPage.loadingTextLabel.text = loadingString;
NSLog(#"LoadingPage text: %#",self.loadingPage.loadingTextLabel.text);
When I checked the console, the NSLog is returning me a <null> value for the loadingTextLable.text. Is there something I am doing wrong here? Note that in my LoadingPageViewController, I hooked up the UILabel called loadingTextLabel in my xib.

use initWithNibName to initialize LoadingPageViewController.

Related

didSelectRowAtIndexPath, pushViewController and a little lable

I have a table controller in which I use didSelectRowAtIndexPath to navigate from the pushed cell to another view. In it I initialize new view controller and push some data in it. After that I do pushViewController.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
ServicesModel *service = [services objectAtIndex:indexPath.row];
ServiceViewController *serviceViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ServiceView"];
serviceViewController.serviceModel = service;
NSLog(#"Set model %#", service.title);
// Pass the selected object to the new view controller.
[self.serviceController pushViewController:serviceViewController animated:YES];
}
In my ServiceViewController I have a label serviceTitle and ServiceModel property for selected service
#property (weak, nonatomic) IBOutlet UILabel *serviceTitle;
#property (strong, nonatomic) ServiceModel *serviceModel;
Using viewDidLoad I'm trying to change text of the label
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"viewDidLoad %#", self.serviceModel.title);
self.serviceTitle.text = self.serviceModel.title;
}
Also I'm trying to access model in viewDidAppear
- (void) viewDidAppear:(BOOL)animated
{
NSLog(#"viewDidAppear %#", self.serviceModel.title);
}
but when view opens, label is empty. Why? What am I doing wrong? The most strange is the log:
(-[ServiceViewController viewDidLoad]) (ServiceViewController.m:43) viewDidLoad (null)
(-[ServicesTableViewController tableView:didSelectRowAtIndexPath:]) (ServicesTableViewController.m:127) Set model Google.com
(-[ServiceViewController viewDidAppear:]) (ServiceViewController.m:36) viewDidAppear (null)
It shows that viewDidLoad fires before I assign the model property. And in viewDidAppear model property is still null. How it can be?
You have two problems. The first one, as 0x7fffffff mentioned, is that you're instantiating your controller incorrectly (it should be initWithNibName:bundle: if made in a xib, and like 0x7fffffff said if in a storyboard).
Second, you can't access the label in serviceViewController from didSelectRowAtIndexPath, because its view has not been loaded yet. So, instead of setting the label in didSelectRowAtIndexPath, you should have a string property in serviceViewController, and give it the value service.text. Then in viewDidLoad, you can populate your label with that string.
Is the label missing altogether, or do you see it and it just didn't receive updated text? If the label is missing, then it's probably a problem in how you're creating the view controller. If for example, you're using storyboards, you should be accessing the view controller like this:
ServiceViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SomeStoryBoardID"];
Instead of this:
ServiceViewController *serviceViewController = [[ServiceViewController alloc] init];
If however, you can see the label, but it just hasn't updated it's text, the first thing you should to is examine the connections inspector in Interface Builder, and verify that the IBOutlet for the label is properly linked.

How do I use a UISegmentedControl to control UITableView cells?

I've successfully set up a Tab Bar controller with. Table View in the first view and a simple UIView in the second tab. The UIView has it's own class. I've added a segmented control to the latter and ultimately I want to change the images used in the table view cells depending on which segment is selected.
The cell images are part of an array and have been set up using a custom cell.
I've set up the segmented control in the UIView class adding #property an #syntesize but I can't for the life of me figure out how to change the cell images from the UIView class.
Thanks in advance!
The segment control in my UIView.h is defined like so
#property (weak, nonatomic) IBOutlet UISegmentedControl *selectedSegment
I've not yet included an IBAction as I'm not 100% where it would need to go or how it would be written.
In my table view my cell images are currently defined like so in cellForRowAtIndexPath
cell.itemImageView.image = [UIImage imageNamed:[icons objectAtIndex:indexPath.row]];
My thoughts were to put a switch statement within the cell method with each case pointing to a different icon set so
switch (something)
{
case 0:
cell.itemImageView.image = [UIImage imageNamed:[iconSetOne objectAtIndex:indexPath.row]];
break;
case 1:
cell.itemImageView.image = [UIImage imageNamed:[iconSetTwo objectAtIndex:indexPath.row]];
break;
default
break;
}
But I'm pretty sure that's the wrong way of doing it and the "something" bit is what I'm stuck on.
1) In the simple UIView where you have your segmented control:
Define an outlet _tableViewController that you connect to the controller of your table view
Define an action method segmentedControlChanged: that responds to the segmented control being triggered
2) In the table view controller:
Define a property selectedSegmentIndex of type NSInteger
3) Now add the implementation for the action method:
- (void) segmentedControlChanged:(id)sender
{
UISegmentedControl* segmentedControl = (UISegmentedControl*)sender;
_tableViewController.selectedSegmentIndex = segmentedControl.selectedSegmentIndex;
[_tableViewController.tableView reloadData];
}
4) In the table view controller you can now add the switch statement that you suggested to tableView:cellForRowAtIndexPath::
switch (self.selectedSegmentIndex)
{
[...]
}
Note that invoking reloadData in step 3 is a crude way to force the table view to update all cells with the correct images. There are other methods in UITableView that give you more fine-grained control over which cells you want updated (e.g. reloadSections:withRowAnimation:).

Set Text Field from another object ios

I am stuck trying to set and retrieve the text in a UITextField object on titleViewController.
I have a Journal object that has a reference to the titleViewController object, but I can not seem to set the text value for the UITextField.
I have an outlet to the UITextField object in the titleViewController, and Journal can see it, but when I write to code to set the value nothing happens when it is run.
There is no errors that pop up in XCode or the Log. I know it should work as the set up works for accessing custom methods in other such viewControllers being managed by the same Journal.
-EDIT-
Here is a code sample as requested:
//Get viewController from Array
TitleVC *titlePage = [_PageArray objectAtIndex:0];
//Get string from Dictionary
NSString *test = [_saveDictionary objectForKey:#"Name"];
//This is one of the attempts to set the UITextField
//FIXED by Tomy211(but still does not work)
[titlePage.textName setText:test];
//This is attempting dot notation
titlePage.textName.text = test
titlePage.textName = test //Wrong I know but had to test
//Also made sure test was a proper string
NSLog(test) //Displayed "Dan" as Expected
This is in Journal.m which accessed a reference to the titleVC from a storyboard using this code:
-(UIViewController *)loadView:(NSString *)viewID{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:viewID];
return viewController;
}
The ViewController returned from that method is used in displaying the View in a scrollview, and I havent had an issue with accessing methods from the ViewControllers this way.
All I want to do is let Journal.m set and read the value from textName which is an outlet connected to a UITextField.
-UPDATE-
I checked and discovered I can read the value of the textField but not write to it.
-FINAL UPDATE-
I found the issue with my code. The code was correct(minus the correction by Tomy211), but the issue was where I was calling the code. I was calling the method before the view was displayed, so when the view did get displayed it revert back to the default values.
Note to all that have similar issues:
Check to make sure the view is being displayed before updating a value of a UIObject part of the view. If you have to update values with the view not displayed, use instance variables and have the UIObject get their values from them on the viewDidLoad method.
The problem was I tried to update the TextField objects before the view has been loaded.
Moving the method call to after the point I load the view fixes the problem. I have updated my question to reflect what was correct, and what was incorrect.
Note to all that have similar issues: Check to make sure the view is being displayed before updating a value of a UIObject part of the view. If you have to update values with the view not displayed, use instance variables and have the UIObject get their values from them on the viewDidLoad method.

Setting title of UINavigationBar

I have an application which uses a main story board to include a Navigation Controller where the main view is a Table View using a prototype cell content. Each cell in the table view pushes onto a new view which I have created with it's own set of .h .m and .xib files.
The table view navigation bar has its title set through the story board which works fine. However, I am having trouble setting the title for each new view after it gets pushed into view.
I have the following in the viewDidLoad method for each view;
self.title = #"View Title";
Any advice?
it is the right way. it will work.
I'm doing something similar in an app I'm working on. Here's how I'm setting the title:
[[self navigationItem] setTitle:#"My View's Title"];
I thinkt the dot notation equivalent would be something like this:
self.navigationItem.title = #"My View's Title";
Hope that helps.
If you have a tab bar controller, try this:
self.tabBarController.navigationItem.title = #"Title";

Pass data to UITextField between ViewControllers

I am stuck on passing data from one ViewController to another. The scenario is as:
I have 2 ViewControllers named : SearchDomainController and LoginViewController. What i am trying to do is pass the string value from SearchDomainController to the UITExtfield in LoginViewController.
In LoginViewController i have declared IBOutlet UITextField *domainField; and also a property #property(nonatomic,retain) UITextField *domainField.
The problem is when i create a new instance of LoginViewController in SearchDomainController and try _loginViewController.domainField.text = #"Some text";
the text never changes in UItextField on LoginViewController.
What did i miss ? And what are the best solution for this kind of problem? Thanx
My guess would be that _loginViewController.domainField is nil at that time, which is probably because the view hasn't loaded yet, and the label is created when the view loads (via a nib) and not as soon as the view controller object is created.
In order to not depend on having the view fully loaded when passing the value, I would have used a separate property for passing along the title, i.e. _loginViewController.domainFieldText = #"Some text";. Then in viewDidLoad of the _loginViewController, assign the value of domaonFieldText to the actual label.
Alternatively, make sure the UILabel instance is created and not nil when you set its text from the other view controller.
Set the string as a property of the AppDelegate in one view controller, and you can retrieve it from another, and set it as textField.text