iPhone:当用户摇动时修改视图
我正在开发一个 iPhone 应用程序,当用户摇动手机时,它会从表视图中删除行。我创建了一个基于导航的项目。现在,当用户摇动 iPhone 时,我希望导航栏的标题更改为“删除”,并在同一视图中的导航栏上显示删除按钮。否则,当用户选择特定行时,它应该移动到下一个视图。我编写了以下代码,但它不起作用。请帮帮我。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (isShaked == NO)
{
//logic to move to next view goes here.
}
else
{
self.title = @"Delete Rows";
delete=[[UIBarButtonItem alloc] initWithTitle:@"Delete rows" style:
UIBarButtonItemStyleBordered target:self action:@selector(deleteItemsSelected)] ;
self.navigationItem.rightBarButtonItem=self.delete;
MyTableCell *targetCustomCell = (MyTableCell *)[tableView cellForRowAtIndexPath:indexPath];
[targetCustomCell checkAction];
[self.tempArray addObject: [myModal.listOfStates objectAtIndex:indexPath.row]];
//[delete addTarget:self action:@selector(deleteItemsSelected:) forControlEvents:UIControlEventTouchUpInside];
self.tempTableView = tableView;
}
}
-(void)deleteItemsSelected
{
[myModal.listOfStates removeObjectsInArray:tempArray];
[tempTableView reloadData];
}
checkAction
方法是一种自定义单元格方法,用于在所选行上放置刻度线。
I am developing an iPhone application which deletes rows from a table view when the user shakes the phone. I have created a navigation-based project. Now when the user shakes the iPhone I want the title of the navigation bar to change to "DELETE" and a delete button to appear on the navigation bar, in the same view. Otherwise, when a user selects a particular row it should move to the next view. I have written the following code but it's not working. Please help me out.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (isShaked == NO)
{
//logic to move to next view goes here.
}
else
{
self.title = @"Delete Rows";
delete=[[UIBarButtonItem alloc] initWithTitle:@"Delete rows" style:
UIBarButtonItemStyleBordered target:self action:@selector(deleteItemsSelected)] ;
self.navigationItem.rightBarButtonItem=self.delete;
MyTableCell *targetCustomCell = (MyTableCell *)[tableView cellForRowAtIndexPath:indexPath];
[targetCustomCell checkAction];
[self.tempArray addObject: [myModal.listOfStates objectAtIndex:indexPath.row]];
//[delete addTarget:self action:@selector(deleteItemsSelected:) forControlEvents:UIControlEventTouchUpInside];
self.tempTableView = tableView;
}
}
-(void)deleteItemsSelected
{
[myModal.listOfStates removeObjectsInArray:tempArray];
[tempTableView reloadData];
}
checkAction
method is a custom cell method which is used to put a tickmark on the row selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了让您检查手机是否被摇晃,您的类必须使用 UIAccelerometerDelegate 协议。
例如:
然后,您需要能够判断手机何时被摇动(我在 viewDidLoad 中使用它):
一旦用户摇动手机,您就可以用这种方法施展魔法:
您显然可以更改检查的时间间隔更频繁地更改加速度计上的参数:didAccelerate 方法以满足您的需求。
In order for you to check if the phone's been shaken, your class will have to use the UIAccelerometerDelegate protocol.
For example:
Then, you need to be able to tell when the phone's been shaken (I use this in my viewDidLoad):
Once the user shakes the phone, you can do your magic in this method:
You can obviously change the interval to check more often and change the parameters on the accelerometer:didAccelerate method to suit your needs.
检查这些方法/API:
这些是为动作识别提供的事件处理程序。使用这些之前请先阅读文档。
Check these methods/APIs:
These are event handlers provided for motion recognition. Go through the document before using these.