Custom Keyboard Problems

Discussion in 'iOS Programming' started by AndrewP234, Jun 20, 2010.

  1. AndrewP234 macrumors newbie

    Joined:
    Jun 20, 2010
    #1
    Hello,

    I am designing a custom keyboard for my app but I am having problems understanding how I can add text to my text field and then dismiss the keyboard without having both classes referencing each other.

    I have a main controller which declares an instance of a inherited UIViewController and a text field, the view of this controller is my custom keyboard added over the default one.
    I have also declared in this class several buttons, one of which I want to set to dismiss the keyboard and the others will add characters to my textbox.
    This class doesn't have access to the textfield which caused my custom keyboard to appear so how do I access it to do the actions above?

    I am guessing it has something to do with delegates but I just cant seem to get my head round it.

    Hope someone can help
    Andrew
     
  2. MokSiFu macrumors member

    Joined:
    Jul 24, 2009
    Location:
    Down Under
    #2
    can you provide a screenshot of what you're trying to do?

    I am also thinking it will require delegating.
     
  3. AndrewP234 thread starter macrumors newbie

    Joined:
    Jun 20, 2010
    #3
    I have attached a screen shot with labels added which attempt to explain what I want to do.

    Hope you can help

    --
    Andrew
     

    Attached Files:

  4. MokSiFu macrumors member

    Joined:
    Jul 24, 2009
    Location:
    Down Under
    #4
    In your CustomKeyboard header file add an instance variable in @interface{ :
    Code:
    id *delegate;
    
    And then add it as a property under your @interface{ ... }
    Code:
    @property (nonatomic, assign) id *delegate;
    
    I suggest making your own protocol and using it, and make your main view controller conform to it. Read up on protocol making.

    Then in your implementation file synthesise it:
    Code:
     
    @synthesize delegate;
    
    I'm assuming you have a CustomKeyboard called in your main view controller, so when your main view controller loads, then in your viewDidLoad method you want to set the delegate to itself:
    Code:
    - (void)viewDidLoad {
            myCustomKeyboard.delegate = self;
    	[super viewDidLoad];
    }
    
    In your CustomKeyboard implementation file you just call methods on the delegate (which is set to your main view controller in this code) like......
    Code:
            [delegate foo:bar];
    
    to make your main view controller execute a method defined inside it called "foo" with argument "bar".

    Now, you might get a warning "blah blah blah might not respond to foo: method"..... which again, read up on protocols to not have that. Or you can dodge it and use the
    Code:
    [delegate performSelector:@selector(foo:) withObject:bar]
    way to do things.

    So if you want to dismiss the keyboard after tapping the equals button you can set up this:
    Code:
    // header file for CustomKeyboard
    ...
    ...
    
    @interface CustomKeyboard : UIViewController {
    	id *delegate;
    }
    
    @property (nonatomic, assign) id *delegate;
    - (IBAction) equalSignPressed: (id) sender;
    
    Code:
    // implementation file for CustomKeyboard
    ...
    ...
    @implementation CustomKeyboard
    @synthesize delegate;
    
    - (IBAction) equalSignPressed: (id) sender {
         [delegate.textField resignFirstResponder];
         [delegate hideCustomKeyboard];
    }
    
    In your main view controller code you might want a hideCustomKeyboard to hide your custom keyboard.

    Hope this helps... (I haven't read over it again)
     

Share This Page