How can you create an accessoryInputView in a Storyboard design?

走远了吗. 提交于 2019-12-05 23:23:56

问题


I am trying to create an accessoryInputView to augment the keyboard with some app-specific keys.

I am using a Storyboard based design approach to keep the the UI separated from the application logic.

I cannot seem to understand how to create the new view and associated it with the textView. Is it possible?


回答1:


You can check this sample project from Apple: https://developer.apple.com/library/ios/samplecode/KeyboardAccessory/Introduction/Intro.html

To achieve the same result that in the sample, you must drag and drop an UIView instance into the "Document Outline" window when your storyboard is open.

It is the window on the left.

Then you have to create an IBOutlet in your controller to access the view and set it as an inputAccessoryView.




回答2:


In case anyone else is trying to do this, let me record what I ended up doing.

I created a new XIB with the accessoryView I wanted in it. I could not figure out how to assign the inputAccessoryView directly into a storyboard design, so I did the following.

I made the fileOwner of the XIB as the controller class that contains the textView that needs the accessoryView and assigned the view to a IBOutlet in that class. That allows me to load the XIB and auto-assign it to the IBOutlet.

Then in the viewDidLoad, I assign the IBOutlet to the textView that needs it. It works well and allows me to do all the UI work in IB and keep the logic in the controller. I guess I can consider setting the inputAccessoryView as 'logic' and not UI design and consider this a good design. Not stellar, since IB does not allow setting the inputAccessoryView directly from a XIB or creating views in a storyboard that are not part of the flow of the app.

The code in viewDidLoad looks like:

- (void) viewDidLoad
{
    ...   
    [super viewDidLoad];
    [[NSBundle mainBundle] loadNibNamed:@"NoteInputAccessoryView" owner:self options:nil];

    _noteTextView.inputAccessoryView=_noteInputAccessoryView;
    ...
}


来源:https://stackoverflow.com/questions/19944029/how-can-you-create-an-accessoryinputview-in-a-storyboard-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!