Articles Tagged 'buttonWithType'

Apple iPhone: Create a custom toggle button

non funzionano quando un UIButton è impostato in modalità UIButtonTypeCustom ! The states UIControlStateSelected or UIControlStateHighlighted not work when a UIButton mode is set UIButtonTypeCustom ! Or rather, do not work (because reserved for other types of button), for example to create a two-state button: toggle note. If we have created two images (and stato1.png stato2.png) for our button, we can proceed as follows:

1
2
3
/ / The header files that we create a global variable for usaremo
/ / Check the toggle were
ToggleFlag BOOL;

Now we create our button:

1
2
3
4
5
6
7
8
9
/ / We create a button and we place it initially in the state "stato1.png"
/ / Edit initWithFrame: (CGRect) {100,100,50,50} with the location and
/ / Size of your image has
; toggleFlag = YES;
[ [ UIButton buttonWithType : UIButtonTypeCustom ] initWithFrame : ( CGRect ) { 100 , 100 , 50 , 50 } ] ; UIButton ToggleButton * = [[UIButton buttonWithType: UIButtonTypeCustom] initWithFrame: (CGRect) {100, 100, 50, 50}];
@ "" forState : UIControlStateNormal ] ; [ToggleButton setTitle: @ "" Forst: UIControlStateNormal];
[ UIImage imageNamed : @ "stato1.png" ] forState : UIControlStateNormal ] ; [ToggleButton setBackgroundImage: [UIImage imageNamed: @ "stato1.png"] Forst: UIControlStateNormal];
self action : @selector ( onToggle : ) forControlEvents : UIControlEventTouchUpInside ] ; [ToggleButton addTarget: self action: @ selector (onToggle:) forControlEvents: UIControlEventTouchUpInside];
toggleButton ] ; [Self.view addSubview: ToggleButton];

When you click on the button will send a message to be managed onToggle :

1
2
3
4
5
6
7
void ) onToggle : ( id ) sender { - (Void) onToggle: (id) sender {
/ / Retrieve pointer to UIButton
( UIButton * ) sender; UIButton ButtonClicked * = (UIButton *) sender;
/ / Execute Toogle
toggleFlag =! toggleFlag;
[ UIImage imageNamed : ( toggleFlag ) ? @ "stato1.png" : @ "stato2.png" ] forState : UIControlStateNormal ] ; [ButtonClicked setBackgroundImage: [UIImage imageNamed: (toggleFlag)? @ "Stato1.png" @ "stato2.png"] Forst: UIControlStateNormal];
}

Continued ...

Apple iPhone 3.0 SDK: Fixed bug on UIButton buttonWithType

As indicated in the post Apple iPhone SDK 3.0: first incompatibility of the Apple SDK 3.0 show different behaviors with respect to release 2.2.1. Support service for developers gave me the answer, and then the solution to the problem. . The answer was that the technical support [UIButton buttonWithType:] already called - inside - the initWithFrame . It follows that, according to the technical support, writing:

Continued ...

Apple iPhone 3.0 SDK: first incompatibility

After installing the 3.0 SDK for Apple iPhone, evidently still not perfectly stable, I immediately found some problems, both in the compilation of the code is in the general XCode. The most important - which I promptly reported with a "home" to Apple, developers at the center - the creation of buttons through code. Here's an excerpt of the code is not compatible with the SDK 3.0:

1
2
3
4
[ [ UIButton buttonWithType : UIButtonTypeRoundedRect ] initWithFrame : CGRectMake ( 0 , 0 , 100 , 40 ) ] ; UIButton * myButton = [[UIButton buttonWithType: UIButtonTypeRoundedRect] initWithFrame: CGRectMake (0, 0, 100, 40)];
@ "Bottone" forState : UIControlStateNormal ] ; [MyButton setTitle: @ "Button" Forst: UIControlStateNormal];
/ / Other settings
myButton ] ; [Self.view addSubview: myButton];

By filling out this code with the SDK 2.2.1 you get a classic button with the label "Button". With the SDK 3.0, the button is created, but the label disappears. la situazione sembra migliorare, nel senso che la label viene resa apparentemente in modo corretto. Using as buttonWithType type UIButtonTypeCustom seems to improve the situation in the sense that the label is apparently made ​​correctly. It is still strange to the sudden failure of the type UIButtonTypeRoundedRect . I am still awaiting a response from Apple ... I just novelty; place!

Continued ...