横向中的 UIActionSheet 的按钮索引不正确
我有一个操作表,在横向方向的 iPhone 上让我感到悲伤。一切都显示得很好,但在横向中,第一个真实按钮与取消按钮具有相同的索引,因此逻辑不起作用。
我尝试使用 initWithTitle: delegate: cancelButtonTitle: virtualButtonTitle: otherButtonTitles: 创建actionSheet,但那是一样的,我当前的代码如下;
UIActionSheet* actionMenu = [[UIActionSheet alloc] init];
actionMenu.delegate = self;
actionMenu.title = folderentry.Name;
actionMenu.cancelButtonIndex = 0;
[actionMenu addButtonWithTitle:NSLocalizedString(@"str.menu.cancel",nil)];
[self addActiveButtons:actionMenu forEntry:folderentry];
[actionMenu showInView:[self.navigationController view]];
[actionMenu release];
addActiveButtons 方法基本上使用如下代码配置要添加的按钮;
[menu addButtonWithTitle:NSLocalizedString(@"str.menu.sendbyemail",nil)];
有时可能有 6 个按钮,因此在横向模式下,actionSheet 会像这样显示;
我的代表这样回应;
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"Cancel Button Index is : %d",actionSheet.cancelButtonIndex);
NSLog(@"Button clicked was for index : %d",buttonIndex);
NSString *command = [actionSheet buttonTitleAtIndex:buttonIndex];
DLog(@"COMMAND IS: %@ for index: %d",command,buttonIndex);
if ([command isEqualToString:NSLocalizedString(@"str.menu.sendbyemail",nil)]) {
// Do stuff here
}
if ( ... similar blocks ... ) { }
}
在所示示例中,我发现 cancelButtonIndex 正如预期一样为 0,但第一个其他按钮的按钮索引也是如此!这意味着,例如,如果我单击第二个(保存到照片)按钮,我的调试输出将如下所示;
取消按钮索引为:0
单击的按钮用于索引:1
命令是:通过电子邮件发送索引:1
我尝试了各种排列现在我抓狂地想知道我错过了什么。我进行了很好的搜索,但人们似乎遇到的其他问题是显示问题,而不是功能问题。
有人能看出我哪里出了问题吗?
PS。我知道这不是最好的 UI 体验,但我认为大多数用户实际上大部分时间都会处于纵向或使用 iPad 版本的应用程序,因此我准备接受横向操作表默认行为,假设我可以让它真正发挥作用!
I have an action sheet that is causing me grief on the iphone in Landscape orientation. Everything displays just fine, but in Landscape, the first real button has the same index as the cancel button and so the logic doesn't work.
I've tried creating the actionSheet using initWithTitle: delegate: cancelButtonTitle: destructiveButtonTitle: otherButtonTitles: but that was just the same, my current code is as follows;
UIActionSheet* actionMenu = [[UIActionSheet alloc] init];
actionMenu.delegate = self;
actionMenu.title = folderentry.Name;
actionMenu.cancelButtonIndex = 0;
[actionMenu addButtonWithTitle:NSLocalizedString(@"str.menu.cancel",nil)];
[self addActiveButtons:actionMenu forEntry:folderentry];
[actionMenu showInView:[self.navigationController view]];
[actionMenu release];
The addActiveButtons method basically configures which buttons to add which it does using code like this;
[menu addButtonWithTitle:NSLocalizedString(@"str.menu.sendbyemail",nil)];
There are perhaps 6 buttons at times so in landscape mode the actionSheet gets displayed like this;
My delegate responds like this;
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"Cancel Button Index is : %d",actionSheet.cancelButtonIndex);
NSLog(@"Button clicked was for index : %d",buttonIndex);
NSString *command = [actionSheet buttonTitleAtIndex:buttonIndex];
DLog(@"COMMAND IS: %@ for index: %d",command,buttonIndex);
if ([command isEqualToString:NSLocalizedString(@"str.menu.sendbyemail",nil)]) {
// Do stuff here
}
if ( ... similar blocks ... ) { }
}
In the example shown, I am finding that cancelButtonIndex is 0 as expected, but so is the button index for the first other button! This means if I click on the second (Save to Photos) button for example, my debug output looks like this;
Cancel Button Index is : 0
Button clicked was for index : 1
COMMAND IS: Send by Email for index: 1
I've tried various permutations and am now tearing my hair out wondering what I'm missing. I've had a good search around but the other problems people seem to be having are display issues, rather than functionality ones.
Can anyone see where I've gone wrong?
PS. I know this isn't the greatest UI experience, but I figure that most users will actually be in portrait most of the time or using the iPad version of the app so I'm prepared to accept the actionsheet default behaviour for landscape assuming I can get it to actually work!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,通过计算我添加了多少个按钮,然后添加取消按钮作为最后一个选项来修复它,所以我的代码如下所示;
希望能帮助其他遇到同样问题的人!
OK, fixed it by counting how many buttons I was adding and then adding the cancel button as the last option, so my code looks like this;
Hope that helps someone else struggling witht the same issue!
尽管我已经将取消按钮作为操作表中的最后一个按钮并相应地设置了其索引,但我遇到了同样的问题。我的问题与“破坏性”按钮有关。经过一番调查,我对这个问题的看法是:
在将 N 个按钮添加到操作表后,它会切换其布局,将“破坏性”按钮放在顶部,将“取消”按钮放在按钮上。中间是一个可滚动视图,其中包括所有其他按钮。其他来源表明这是一个表视图。
对于 iPhone,纵向方向的 N 为 7,横向方向的 N 为 5。这些数字适用于所有按钮,包括“取消”和“破坏性”。
您最初将“取消”和“破坏性”按钮放置在操作表中的哪个位置并不重要。达到限制后,“破坏性”按钮将移至顶部,“取消”按钮将移至底部。
问题是指数没有相应调整。因此,如果您最初没有将“取消”添加为最后一个按钮,将“破坏性”添加为第一个按钮,则 actionSheet:clickedButtonAtIndex: 中将报告错误的索引,如初始报告所述。
因此,如果您的操作表中要包含 N 个以上的按钮,则必须将破坏性按钮添加到操作表中,作为操作表中的第一个按钮。您必须添加取消按钮作为添加到操作表的最后一个按钮。最初构建工作表时,只需将两者保留为零,如另一个答案中所述。
I ran into the same issue even though I already was including the Cancel Button as the last one in the action sheet and setting its index accordingly. My problems had to do with the 'Destructive' button. After some investigation, here is my take on the problem:
After N buttons have been added to the actionsheet, it switches it's layout to put the Destructive button at the top and the Cancel button at the button. In between is a scrollable view that includes all of the other buttons. Other sources indicate that this is a a table view.
For the iPhone, N is 7 for Portrait orientation and 5 for Landscape orientation. Those numbers are for all buttons including Cancel and Destructive.
It does not matter where in the action sheet you had originally put the Cancel and Destructive buttons within the action sheet. Once the limit has been reached, the Destructive button is moved to the top and the Cancel is moved to the bottom.
The problem is that the indices are not adjusted accordingly. So, if you did not initially add the Cancel as the last button and the Destructive as the first, the wrong index will be reported in actionSheet:clickedButtonAtIndex: as the initial report stated.
So, if you are going to have more than N buttons in your action sheet you MUST add the Destructive button to the actionSheet as the first button to the action sheet. You MUST add the Cancel button as the last button added to the action sheet. When initially constructing the sheet just leave both as nil, as described in another answer.
我也有同样的问题。为了解决这个问题,我只需为所有按钮创建一个包含 nil 的 actionSheet,然后手动添加按钮。最后,在处理程序中,忽略firstOtherButtonIndex,因为它将是错误的(即使您提前设置了它)。相反,假设它是 1,因为索引 0 是本例中的取消按钮。代码如下:
另外,如果您使用的是 iPhone,请不要忘记从选项卡视图中显示此内容,因为选项卡栏会窃取触摸事件并防止按下下方按钮。
I had the same problem. To fix it, I just create an actionSheet with nil for all the buttons, and added buttons manually afterwards. Lastly, in the handler, ignore the firstOtherButtonIndex because it will be wrong (even if you set it ahead of time). Instead, assume that it is 1 because index 0 is the cancel button in this example. Here's the code:
Also, don't forget to show this from a tab view if you're on an iPhone because the tab bar steals touch events and prevents the lower button from being hit.
我的解决方案是像这样初始化,仅指定 structuralButtonTitle...
这样,您始终可以在索引 0 处获得“取消”按钮,并且即使存在滚动视图,您自己的按钮也从索引 1 开始。
My solution is to initialize like this specifying only the destructiveButtonTitle...
That way you get the Cancel button at index 0 always and your own buttons begin at index 1 even when there is a scroll view.