UIButton - alloc initWithFrame: vs. buttonWithType:

前端 未结 6 1103
滥情空心
滥情空心 2020-12-06 17:13

Given (arbitrarily):

CGRect frame = CGRectMake(0.0f, 0.0f, 100.0f, 30.0f);

What\'s the difference between the following two code snippets?<

6条回答
  •  春和景丽
    2020-12-06 17:39

    The main (maybe the only) difference is in memory management: as you said, buttonWithType returns an autoreleased UIButton. This way you don't have to worry about releasing it. On the other hand, you don't own it, so you cannot release it when you want to (except, of course, if you drain the autorelease pool).
    Calling explicitly [[UIButton alloc] initWithFrame:frame], instead, you dynamically alloc your button, so you own it and you're responsible for releasing it.
    If you plan to retain your button for some reason then maybe you should consider the second solution, but if, as in this case, you are autoreleasing it immediately, there's no big difference between the two way of creating a button...

提交回复
热议问题