UIAlertView-Blocks

地址:https://github.com/jivadevoe/UIAlertView-Blocks

HOW TO USE IT

Typically, you'll create items that represent the buttons and the actions to take when they are tapped. For example imagine a dialog box confirming deletion of an item:

RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"No" action:^{

// this is the code that will be executed when the user taps "No"

// this is optional... if you leave the action as nil, it won't do anything

// but here, I'm showing a block just to show that you can use one if you want to.

}];

RIButtonItem *deleteItem = [RIButtonItem itemWithLabel:@"Yes" action:^{

// this is the code that will be executed when the user taps "Yes"

// delete the object in question...

[context deleteObject:theObject];

}];

The label property on the button items is the text that will be displayed in the button.

Once you've created these, you simply initialize your UIAlertView using the initializer, passing your button items accordingly:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Delete This Item?"

message:@"Are you sure you want to delete this really important thing?"

cancelButtonItem:cancelItem

otherButtonItems:deleteItem, nil];

[alertView show];

Alternatively you can also use a single line to create buttons and declare the alert view

[[[UIAlertView alloc] initWithTitle:@"Delete This Item?"

message:@"Are you sure you want to delete this really important thing?"

cancelButtonItem:[RIButtonItem itemWithLabel:@"Yes" action:^{

// Handle "Cancel"

}]

otherButtonItems:[RIButtonItem itemWithLabel:@"Delete" action:^{

// Handle "Delete"

}], nil] show];

Again, this is designed to be fire and forget, so you initialize it, show it, and release it. It'll take care of cleaning up after itself.

You can also add a RIButtonItem to the UIAlertView after initialization, just like you normally would:

[alertView addButtonItem:deleteItem];

This is useful if building an UIAlertView, or UIActionSheet dynamically from an Array:

for (RIButtonItem *item in buttonItemArray) {

[alertView addButtonItem:item];

}

That's it!

The UIActionSheet category works virtually the same as the UIAlertView. Just check out the header for the initializer you need to use. It's very straightforward.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容