Passing String with Notification and than pass to class

Discussion in 'iOS Programming' started by kikko088, Nov 15, 2012.

  1. kikko088 macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #1
    I have a question I work for an ipad application, in this app on main view I have an instance of a custom uitableviewcontroller, on row click I post a notification that pass an object and run a method

    Code:
    - (void) drawWebView:(id) sender {
    
        NSDictionary *dict = [[sender userInfo] copy];
        self.objTesto = [dict objectForKey:@"Testo"];
        self.txtWeb = self.objTesto.testo;
    
        //[self setWebView:self.txtWeb];
        [self.textWebView loadRequest:[self creaFileHTML:text]];
    }
    while I set the load request on the webview of my class all work, If I creat a subclass of uiwebview with a method setWebView: whit a simple NSLog()

    Code:
    [self setWebView:self.txtWeb];
    //[self.textWebView loadRequest:[self creaFileHTML:text]];
    I get this error:

    Code:
    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString userInfo]: unrecognized selector sent to instance 0xee3a1e0'
     
  2. Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #2
    What class do you expect "sender" to be at this point? Obviously not an NSString, but it looks like that's what it is. NSString has no "userInfo" method, and that's why it crashes.

    It's usually a bad idea to declare a method paramater as the "id" type, because you lose type safety.
     
  3. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #3
    the sender is an object, here I get the string:

    Code:
    self.objTesto = [dict objectForKey:@"Testo"];
    self.txtWeb = self.objTesto.testo;
    

    I think, if I use it without an external class all work.
     
  4. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #4
    the problem was that I init the wrong class (I have two similar class) - stupid error -.- excuse me!
     
  5. Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #5
    That's why you should avoid declaring variables and parameters as type "id". These sorts of errors would get picked up by the compiler, instead of causing crashes at runtime!
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I just wanted to say that it is pretty common for the sender parameter of an IBAction to be data-typed as an id.
     

Share This Page