简体   繁体   English

iOS Dev via Xcode:UIView&UITextView / UIPickerView - 无法分配代理?

[英]iOS Dev via Xcode: UIView & UITextView/UIPickerView - Cannot Assign Delegate?

Up until updating Xcode last night, this was working great (though probably through sheer dumb luck!). 直到昨晚更新Xcode,这个工作得很好(虽然可能是因为运气不好!)。

I'm getting an error now for my UITextViews, Fields and UIPickerViews when assigning the delegate: 在分配委托时,我现在为我的UITextViews,Fields和UIPickerViews收到错误:

[m_textField setDelegate:GAMESTATE->glView.self];

m_textField is defined as UITextField*. m_textField定义为UITextField *。 glView is defined as a UIView*. glView被定义为UIView *。 To keep things simple, I'm using just one UIView for the entire app (could also be a problem). 为了简单起见,我在整个应用程序中只使用了一个UIView(也可能是个问题)。 I only need access to a couple of basic operations in the app to get simple text info from the user. 我只需要在应用程序中访问几个基本操作,即可从用户那里获得简单的文本信息。 Now I get the error: 现在我收到错误:

Cannot initialize a parameter of type 'id<UITextFieldDelegate>' with an rvalue of type 'UIView*'

Like I said, I probably was doing something wrong in the first place, and it only worked through sheer, dumb luck! 就像我说的那样,我可能在第一时间做错了什么,它只能通过纯粹,愚蠢的运气来解决! If it helps, I'm already subclassing my UIView as an Accelerometer Delegate, as such: 如果它有帮助,我已经将我的UIView子类化为Accelerometer委托,如下:

@interface GLView : UIView <UIAccelerometerDelegate>

Any help, suggestions or tips would be greatly appreciated. 任何帮助,建议或提示将不胜感激。 I've stumbled through some books this morning and brushed up on several Apple docs, but a quick work-around doesn't seem possible. 今天早上我偶然发现了一些书籍,并刷了几本Apple文档,但似乎没有快速的解决方法。 I'm not adverse to refactoring the way I take input from the user, I could just use a hand getting there. 我并不反对重构我从用户那里获取输入的方式,我可以用一只手到达那里。

You should create, for example, NSObject that will delegate 例如,您应该创建将委派的NSObject

  1. for UITextView : UITextViewDelegate . 对于UITextViewUITextViewDelegate
  2. for UIPickerView : UIPickerViewDelegate 对于UIPickerViewUIPickerViewDelegate

Your class declaration should look like this: 您的类声明应如下所示:

@interface MyViewDelegator:NSObject<UITextViewDelegate, UIPickerViewDelegate>

Then you should alloc+init it and set it as delegator of your text/picker views: 然后你应该分配+ init并将其设置为文本/选择器视图的委托者:

MyViewDelegator *delegator = [[MyViewDelegator alloc] init];
[m_textField setDelegate:delegator];

Don't forget to implement appropriate methods in MyViewDelegator that are not optional for protocols. 不要忘记在MyViewDelegator中实现对协议不可选的适当方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM