开发者

Cocoa:Testing for the same object with ifs/switches

Ok so here's my code, it works great:

- (void)textViewDidChange:(UITextView *)textView{
 if (textView==someObject) {
  [detailItem setValue:textView.text forKey:@"someObjectAttrib开发者_如何学Goute"];
 }

The problem is that I have lots of textviews instances to test for and I would prefer to find some other way to consolidate the code. I was thinking of something like a switch but I don't see how that would work. Any ideas?


One way would be to use the integer tag of each view. In your code, you’d have an enum like:

enum
{
    kThingView,
    kOtherView,
    ...
};

Each view's tag is set appropriately in IB or when setting up the view programatically. Then:

- (void) textViewDidChange:(UITextView *)textView
{
    switch (textView.tag)
    {
        case kThingView:
            ...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜