开发者

Cocoa:Testing for the same object with ifs/switches

开发者 https://www.devze.com 2022-12-29 05:06 出处:网络
Ok so here\'s my code, it works great: - (void)textViewDidChange:(UITextView *)textView{ if (textView==someObject) {

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

精彩评论

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

关注公众号