0

I have a Date displayed in a UIlabel in my main view controller with a date picker that changes the date in the UILabel. I have UIWebViews that change based on the date. How do I pass the date displayed in the UILabel to the UIWebview?

Thanks in advance

Popeye
  • 11,839
  • 9
  • 58
  • 91
Mina Dawoud
  • 408
  • 8
  • 22
  • Well the first thing you need to learn is that this has nothing to do with the `xcode IDE`. – Popeye Dec 17 '13 at 15:22

1 Answers1

0

Add the target and selector for event: UIControlEventValueChanged and then simply call the javascript function from the selector.

//set the selector for datePicker event
[datePicker addTarget:self action:@selector(dateChanged)  forControlEvents:UIControlEventValueChanged]; 
.......
.......
-(IBAction)dateChanged
{
     .......
     .......
     //call the javascript function text=>data to show
     NSString  *string = [NSMutableString stringWithFormat:@"showTextFromNative( %@)", text];    
     [self.webviewOutlet stringByEvaluatingJavaScriptFromString:string];
}


//javascript function in your html
function showTextFromNative(text) 
{ 
 ....
 ....///Do what ever you want to show in webview
 ....
}
Ashish Bindal
  • 995
  • 1
  • 8
  • 16
  • Why are you doing `NSString *string = [NSMutableString stringWithFormat:@"showTextFromNative( %@)", text];`? 1) It doesn't need to be `NSMutableString` it can just be `NSString` and you could drop it all to one line `[self.webviewOutlet stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"showTextFromNative( %@)", text]];` – Popeye Dec 17 '13 at 21:19
  • The local html files are simple readings that change everyday. I want it so the when the date changes in my nsdate in the view controller, all the web views change as well to the appropriate date. – Mina Dawoud Dec 18 '13 at 05:37
  • @Popeye yeah it could be nsstring and merged with in single line of code. But what's the point ? BTW i usually prefer mutable objects in case they need to modify somewhere down the road. – Ashish Bindal Dec 18 '13 at 09:10
  • @MinaDawoud I guess I don't understand your question exactly. What I have understood is you want to show the selected date (by date picker) in web view(s). If this is not the question, could you explain or rephrase your problem? (local html files are simple readings ..?) – Ashish Bindal Dec 18 '13 at 09:28
  • @AshishBindal you're passing it to an `NSString*` object so you will not be able to modify later down the line. If you want that then you need to it to be `NSMutableString *string = [NSMutableString ....];` but there is no point using pointless memory up just use `NSString *` and if it needs modifying which is unlikely just reassign it. And less code is always better so there is a point to making it a single line. – Popeye Dec 18 '13 at 14:55
  • @Popeye I would prefer to write code on stackoverflow which is clear and easy to understand for the naive person. Btw, Thanks for trivial explanation about the NSString. However just to correct you, mutable object can be modified by recasting even if It is assigned to NSString*. – Ashish Bindal Dec 18 '13 at 16:03
  • I don't know anyone that would do `NSString *string = [NSMutableString ...]` there is no point you would ever do `NSString *string = [NSString ...]` or `NSMutableString *string = [NSMutableString ...]` and whilst we're at it convention says you should be using `string` as a variable - nothing stopping you just convention. – Popeye Dec 18 '13 at 16:23
  • @AshishBindal Im sorry for the delayed response, I hope you had a nice holiday..I have a local HTML file in a UIWebView that changes content based on the date. I also have a UILabel that displays the current date and a UIDatePicker that changes the date on the UILabel, I want it so the HTML file displays the appropriate content based on the date the user selected. It works if I change the system date in settings but I want the same effect within my app. – Mina Dawoud Jan 02 '14 at 21:58
  • @MinaDawoud I was wondering if you have tried the solution I wrote because that's exactly I have been doing in the code. – Ashish Bindal Jan 03 '14 at 00:56
  • Yes, html setup is a lot more complicated than simple text. This will not work. – Mina Dawoud Jan 03 '14 at 03:43