passing data from uiview to tabview - tabview

I have a variable 'userID' that is getting populated after validating through a parser. Now when I will press the login button, the next view will be a tabView (Consisting of 5 screens). Now I want this userID to be fixed for UILabel in all tabViews. How to do this ?
Thanks in advance.

You can store it into NSUserDefaults using:
[[NSUserDefaults standardUserDefaults]setObject:userID forKey:#"USERID"];
and you can get its value anywhere using:
[[NSUserDefaults standardUserDefaults]objectForKey:#"USERID"]]

Related

TextField on Custom Cells

I am using custom cell inside tableView. In that custom cell, there is label and textField....this custom cell is being reused to display ten items. I actually want to open a datepicker on one of the cell's text field but I am not able to understand that how this work can be achieved?
All you need to do is to create an instance of UIDatePicker and set it as inputView of one of your text fields. When the text field is tapped UIKit will animate appearance of the picker for you. In turn, you'll have to handle user's input and update the text field's text.
Please set the text field tag and delegate in cellForRowAtIndexPath method...
like..
cell.yourTextFieldName.tag = 1000;
cell.yourTextFieldName.delegate = self;
In .h file add the UITextFieldDelegate
Then in the textFieldShouldBeginEditing delegate you can get the picker.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSInteger txtFldTag = textField.tag;
if(txtFldTag == 1000){
//write the method to show picker.
return NO;
}

How to pass UILabel with NSUserdefualts?

I'm trying to pass the value of UILabel with NSUserDefaults .
At the first viewController I have a picker and the picked item is a UILabel string.
In the second viewController I want the item that has been chosen from the picker to be simply displayed as a UILabel string .
How can I do this with NSUserDefaults, I know how do it with UITextField but it does not work for me with UILabel
Why does this happen?
Solution
To store the value of your UILabel to NSUserDefaults you can use
[[NSUserDefaults standardUserDefaults] setObject:label.text forKey:#"myLabelText"];
To get the value stored on NSUserDefaults and pass it to label you can use
label.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"myLabelText"];
BUT
NSUserDefaults is not recommended to store dynamic properties. The easiest solution for you should be to pass data between different viewControllers:
Passing Data between View Controllers

Prevents UISearchDisplayController from hiding the UiTable

Im new to objC and Im currently experimenting on UISearchDisplayController. Basically I have an array of string as my data and I use UISearchBarDisplayController to filter my data. Im able to retrieve the correct values when I enter my searchText into the search bar. However, the tableView disappears when my searhBar text is empty.
Would it be possible to prevent the tableView from hiding in this such case. What I want is to just to display all the values in my array in the table if the searchBar text is empty.
I checked the hidden/alpha/frame property of the table and tried to fix my issue here but I think Im in the wrong path here. I'm thinking if i need to subclass the UISearchDisplayController and override the [setActive:YES animated:YES];? Any hint would be appreciated.
You should recieve an event for any change to the search parameters, including when the string is empty.
If you change your implementation of that delegate method to check if the string is empty, you can return the original data instead of the filtered data. This should achieve what you're asking for without the need for subclassing.
I ended up working with UISearchBar and UITable.
So here are the scenarios I encountered.
Display the the UITable when the searchBar is clicked.
UITable shows all the data from my plist when searchText is empty.
UITable shows filtered results from my plist that matches the searchText.
Dismiss the keyboard on press of the search button but don't disable the cancel button on the searchBar.
Remove the UITable when cancel button is press.
I don't have the animation for showing the table for now but this works for me. Also, I allowed the user interaction and scrolling on my table during search so no overlay needed in this case. Glad its working now. :)
I created a sample project for those who might need. Here is the link. Apologies for a messy code and leaks issues. Just posted this to share. :)
You could try by always leaving a zero-space width characters in the search textfield :
static NSString* zsp = #"\u200B";
//In the UISearchBarDelegate
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if(searchBar.text.length == 1 && [text isEqualToString:#""])
{
searchBar.text = zsp;
return NO;
}
return YES;
}

How to change value of a UITextfield on a UIButton click?

I have two text fields and a button on my view.
In the first text field, user enters some value in celsius and by clicking on the button, user is able to find the fahrenheit value on the other textfield. This works fine.
However, when user enters another celsius value in the first textfield, at the touch of the button the fahrenheit value is APPENDED to the previous fahrenheit value, instead of changed.
in your button action function
farenheitTextField.text=nil;
[farenheitTextField setClearsOnBeginEditing:YES];
farenheitTextField.text=#"your calculated value";
Try with this...u need to call this method
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
return YES;
}
Also u need to set the previous value of ur 2nd textfield to nil in the Button Action.
On button click event just write text2.text = #"";
and after that write your entire code.
Use the delegates function of textfield and just assign a empty NSString to your NSString that contains the value of the first textfield.
As in: nsstring *demoStr = #"";
Now it will pick the fresh value each time .
Try changing the value of the textField to #"" before you assign the new value. All this on the IBAction method.
You may want to try this on button click event:
textField.text = #"YourString";
Hope this helps.

Unable to set the UILabel of a view Controller from a different view Controller

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.