在objective-c中发送消息时可以进行强制转换?

Ser*_*nce 0 iphone cocoa-touch casting objective-c ios

我有:

[[[[self navigationItem] leftBarButtonItem] customView] setTitle:@" Create "];
Run Code Online (Sandbox Code Playgroud)

这触发了警告"'UIView'可能无法响应'-setTitle:'".我试过了:

[[[[self navigationItem] leftBarButtonItem] (UIButton*)customView] setTitle:@" Create "];
Run Code Online (Sandbox Code Playgroud)

当我这样做时会出错.0I也尝试使用(id)进行投射,但也没有用.我知道我可能只是将customView存储在UIButton中并从那里开始,但只是想知道是否可以在这样的消息中进行转换?

Pen*_*One 5

想想你在铸造什么.试试这个

[(UIButton*)[[[self navigationItem] leftBarButtonItem] customView] setTitle:@" Create "];
Run Code Online (Sandbox Code Playgroud)

这一行和你的行的不同之处在于你不应该强制转换属性(例如customView),而是抛出要调用方法的返回对象.