(iOS/Objective-C) 有条件地向 UIActionSheet 添加不同按钮的更好方法?
在我的 iOS 应用程序中,我向用户呈现一个 UIActionSheet;然而,UIActionSheet 必须能够针对不同情况呈现不同的按钮(例如,如果用户没有运行 iOS 5,则 Twitter 支持不可用,因此不要显示“Tweet this”按钮;如果 AirPrint 不可用,则用户可以不打印,所以不要显示“打印”按钮等。)现在我以一种非常脑死亡的方式实现了这一点,基本上使用了一堆 if-then-else 语句(见下文)。有没有更干净的方法来做到这一点?
if(NSClassFromString(@"TWTweetComposeViewController")) {
if ([TWTweetComposeViewController canSendTweet]) {
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), NSLocalizedString(@"Tweet This", @"Tweet This button"), nil];
} else {
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), nil];
}
} else {
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), nil];
}
In my iOS app, I'm presenting a UIActionSheet to a user; however the UIActionSheet has to be able to present different buttons for different cases (e.g. if user is not running iOS 5, then Twitter support is unavailable, so don't show the "Tweet this" button; if AirPrint is unavailable, then user can't print, so don't show the "print" button, etc.) Right now I have this implemented in a really brain-dead fashion, basically using a bunch of if-then-else statements (see below). Is there a cleaner way of doing this?
if(NSClassFromString(@"TWTweetComposeViewController")) {
if ([TWTweetComposeViewController canSendTweet]) {
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), NSLocalizedString(@"Tweet This", @"Tweet This button"), nil];
} else {
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), nil];
}
} else {
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Options", @"Options string")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Open in Safari", @"Open in Safari button"), NSLocalizedString(@"E-mail to a Friend", @"E-mail to a Friend button"), NSLocalizedString(@"Print", @"Print button"), nil];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与使用
addButtonWithTitle:(NSString *)title
一样简单:This is about as simple as you're gonna get using
addButtonWithTitle:(NSString *)title
: