Reputation: 19
How to create UIButton dynamically in iOS.
UIButton *btn=[[UIButton alloc]init];
btn.frame=CGRectMake(0,20,30,30);
btn.setTag=2;
[self.view addSubView:btn];
I have tried this code. My Question is can I give negative value for tag value? Like
btn.setTag=-2;
Is it possible to set it in dynamic UIButton ?
Upvotes: 0
Views: 550
Reputation: 17595
Yes, you can set. Because tag
type is NSInteger
. If it is NSUInteger
means, you cann't
set. But in this, you can.
But instead of btn.setTag=-2;
, btn.tag = -2
. You can do like this.(it will explicitly call [btn setTag:-2]
via setter method.)
Upvotes: 1
Reputation: 17535
Yes, You can set in 2 ways...
First Solution
btn.tag=-2; // -------- this is property ----------
Second Solution
[btn setTag:-2]; // -------- this is method ----------
Upvotes: 1