0

I have a UIPickerView in a XIB and I have events that modify other elements within this view:

- (void) pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger) component {
    [self.definitionBeep play];
   self.glossaryTerm.text = [[self.dataSource objectAtIndex:row]objectAtIndex:0];
   self.glossaryDefinition.text = [[self.dataSource objectAtIndex:row]objectAtIndex:1];
}

I then have another View, not built with IB, built programmatically that is accessed from my ViewController.m as follows:

- (IBAction)someTouchElement:(id)sender{

    if(myWebScroller == nil) {
        myWebScroller = [[MyWebScroller alloc]
                               initWithFrame: CGRectMake(15, mainControl.frame.origin.y, mainControl.frame.size.width, mainControl.frame.size.height)
                               title:@"     MY TITLE"
                               category:myCat];
        [self.view addSubview: myWebScroller];
    }
}

Within MyWebScroller, I'm listening for clicks and am able successfully intercent them. What I want to do is change the value of the UIPickerView and have it change the value of the glossaryTerms's as shown above.

If you need more clarification, please comment and I'll update the question accordingly. Thanks

4
  • Don't know if I understood correctly, is it not enough to expose the UIPickerView as IBOutlet in controller ? Or mark it with a specific tag, either in IB or at code creation time myPickerView.tag=myCUstomTag, and then later [yourViewContainingPicker viewWithTag:myCustomTag] ?
    – Leonardo
    Aug 23, 2012 at 14:55
  • The problem is I'm listening for events (within the webview) in MyWebScroller.m which isn't part of ViewController.m
    – Shamoon
    Aug 23, 2012 at 15:01
  • 1
    uhm, I had a similar problem with an iPad app, when a change on a controller should fire a change in other controller, I ended up launching a custom notification after changing value, and let the other observer controller react to this. If this is your case I can post some code.
    – Leonardo
    Aug 23, 2012 at 15:10
  • Sure... please post as an answer
    – Shamoon
    Aug 23, 2012 at 15:40

1 Answer 1

2

Extending what Leonard asked you to do ..

To listen for notification, you need to register an observer for it. so you can do it either in ViewDidLoad: or in ViewWillAppear of Viewcontroller.m

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NotificationPosted:) name:@"ClickedInWebView" object:nil];

then in Viewcontroller.m. implement the method "Notification Posted"

-(void)NotificationPosted:(NSNotification *)notification {

NSLog(@"Something happened");
//change the picker here.
 }

After this, in webView.m, you can post notification whenever you want to change the value in picker view.

[[NSNotificationCenter defaultCenter] postNotificationName:@"ClickedInWebView" object:nil userInfo:nil]

;

Alternatively, you can also pass the NSString or NSNumber via NSDictionary.

NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:value, @"keyName", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"ClickedInWebView" object:nil userInfo:userInfo];

Then you can use this dictionary in viewcontroller in that implemented method. use notification.userinfo .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.