NimbleKit - fast iOS app development

Access a UITextField using IBOutlet's?

Ask questions and share your skills here

Access a UITextField using IBOutlet's?

Postby tarnfeld » Fri Jan 01, 2010 2:40 am

Is there any way of accessing a textfield that has been implemented onto the view via NKTextField?
tarnfeld
 
Posts: 189
Joined: Mon Jun 15, 2009 9:37 pm

Re: Access a UITextField using IBOutlet's?

Postby sunny » Fri Jan 01, 2010 6:36 pm

any native control added to webview is a subview for it's view, you can obtain all subviews and get your textfield by checking [subview isKindOfClass:[UITextField class]], this solution is bad but only at the moment
User avatar
sunny
Staff
 
Posts: 2666
Joined: Sat May 30, 2009 5:18 am
Location: Palo Alto, California

Re: Access a UITextField using IBOutlet's?

Postby tarnfeld » Fri Jan 01, 2010 6:51 pm

So how would i implement this, i know that to get a value i do this:

Declare in .h file
NSString *myFieldVal = [textfield text];

But how do i declaimer "[subview isKindOfClass:[UITextField class]]"
tarnfeld
 
Posts: 189
Joined: Mon Jun 15, 2009 9:37 pm

Re: Access a UITextField using IBOutlet's?

Postby sunny » Fri Jan 01, 2010 7:07 pm

well, you have webview in your obj-c/js mix code, you can call method subviews to get array of all subviews then you need to make loop
for (id subview in subviews)
{
if ([subview isKindOfClass:[UITextField class]]) {
NSLog("This subview is a text field")
// actions on that subview here
}
}
User avatar
sunny
Staff
 
Posts: 2666
Joined: Sat May 30, 2009 5:18 am
Location: Palo Alto, California

Re: Access a UITextField using IBOutlet's?

Postby tarnfeld » Fri Jan 01, 2010 7:10 pm

Here is my code, in my appdelegate file:
Code: Select all
- (void)urlScheme:(NSURL *)url {
   
   for (id subview in _webView)
   {
      if ([subview isKindOfClass:[UITextField class]]) {
         //NSLog("This subview is a text field")
         [(UITextField *)subview setText:@"MyString"];
      }
   }
   
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
   [self urlScheme:url];
    return YES;
}


So this should set the text as @"MyString" ?
tarnfeld
 
Posts: 189
Joined: Mon Jun 15, 2009 9:37 pm

Re: Access a UITextField using IBOutlet's?

Postby sunny » Fri Jan 01, 2010 8:45 pm

well, if it was working you would not post this message, so no, it's no working :)
as I said before " you can call method subviews to get array of all subviews" and then use obtained array in the loop
NSArray* subviews = [_webView subviews];
for (id subview in subviews)
....
User avatar
sunny
Staff
 
Posts: 2666
Joined: Sat May 30, 2009 5:18 am
Location: Palo Alto, California

Re: Access a UITextField using IBOutlet's?

Postby tarnfeld » Fri Jan 01, 2010 9:57 pm

Haha!
Well the app loads up now with this code, but the textfield doesn't work... alternative would be to call a function and pass it the NSUrl ?
Is this complicated? :)
tarnfeld
 
Posts: 189
Joined: Mon Jun 15, 2009 9:37 pm

Re: Access a UITextField using IBOutlet's?

Postby tarnfeld » Fri Jan 01, 2010 10:06 pm

I think the HUGE issue im having here is... well, this is for URLSchemeing - so it is called way before the javascript shows a UITextField - so how could i modify this code to call my
Code: Select all
[self  urlScheme:url];
function after the
Code: Select all
_mainWebViewLoaded
?
tarnfeld
 
Posts: 189
Joined: Mon Jun 15, 2009 9:37 pm

Re: Access a UITextField using IBOutlet's?

Postby tarnfeld » Sat Jan 02, 2010 2:04 am

Ok,
This, it doesn't work.
Code: Select all
-(void)stringCaller:(NSURL *)url
{
   //URLString = [url absoluteString];
   URLString = @"Hello!";
   NSArray* subviews = [_webView subviews];
   for (id subview in subviews)
   {
      if ([subview isKindOfClass:[UITextField class]]) {
         NSLog(@"This subview is a text field");
      }
   }
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
   [self stringCaller:url];
   return YES;
}


If i call NSLog(@"bleee"); with the for statement commented out, it works fine by NSLogging, but for some reason it doesnt with the for? And yes i did lots of debugging and placed NSLog(@"1st"); -> 4th around the delegate to see in what order things happen, and these functions ^ are called last, even after the page is loaded... i have put this:
Code: Select all
while (!_mainWebViewLoaded) {
      [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
   }
   NSString *called = @"1";
   while (_mainWebViewLoaded && called!=@"0") {
      NSLog(@"loaded");
      called = @"0";
   }

So i can see when the view is loaded in comparison to the commented out code and an NSLog ^ but it always comes after.. so in reality it should work as there is indeed a UITextField subview?

Also, does this break the rules of apple's API so possible rejection?
tarnfeld
 
Posts: 189
Joined: Mon Jun 15, 2009 9:37 pm

Re: Access a UITextField using IBOutlet's?

Postby sunny » Sat Jan 02, 2010 6:28 am

probably it will be way faster if you just send me the code and I'll fix bugs sunny@nimblekit.com
User avatar
sunny
Staff
 
Posts: 2666
Joined: Sat May 30, 2009 5:18 am
Location: Palo Alto, California

Next

Return to How To...

Who is online

Users browsing this forum: No registered users and 3 guests