我想在我的iOS应用程序中设置一些本地通知,但是我似乎在实现它们时发现的每个教程似乎只允许我根据计时器发出通知(参见下面的示例代码)?是否可以在将新数据加载到UITableView时触发本地通知?很抱歉一般的问题,但我似乎无法找到很多关于此的文档.如果我的用户点击屏幕时只抓取数据,我也不确定如何才能做到这一点?例如,在ViewController的viewDidLoad中抓取/更新数据?
-(void)viewDidLoad {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:@"Alert Fired at %@", dateTime];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Run Code Online (Sandbox Code Playgroud) 当我的键盘被激活时,我正在使用以下代码来移动视图和我的tableview.然而,当键盘关闭时,在键盘关闭后2秒内将upView固定为实时(另一方面,tableView是即时的).为什么会这样?
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardWillChange:(NSNotification *)notification {
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
UITabBarController *tabBarController = [UITabBarController new];
CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;
self.keyboardHeight = keyboardFrameBeginRect.size.height - tabBarHeight;
}
- (void) animateTextView:(BOOL) up
{
const int movementDistance = self.keyboardHeight;
const float movementDuration = 0.2f;
int movement= movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView …Run Code Online (Sandbox Code Playgroud) 我正在尝试在成功进行应用购买交易时将数据发布到我的数据库.也就是说,当我使用下面的代码时,XCode会抛出错误:
"无法从switch语句跳转到此案例标签"
我怎样才能解决这个问题?见下面的代码.
.M
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch(transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");
break;
case SKPaymentTransactionStatePurchased:
self.indicatorThree.hidden = YES;
[self.indicatorThree stopAnimating];
self.indicatorTwo.hidden = YES;
[self.indicatorTwo stopAnimating];
self.indicatorThree.hidden = YES;
[self.indicatorThree stopAnimating];
NSMutableDictionary *viewParams3 = [NSMutableDictionary new];
[viewParams3 setValue:@"current_user" forKey:@"view_name"];
[DIOSView viewGet:viewParams3 success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.currentUser = [responseObject mutableCopy];
NSString *pointBalance = self.currentUser[0][@"points balance"];
NSMutableDictionary *userData = [NSMutableDictionary new];
int result = [pointBalance intValue] + [self.pointsPurchase intValue];
NSLog(@"THIS IS THE RESULT POINTS …Run Code Online (Sandbox Code Playgroud) 我有一个NSMutableArray内部AppDelegate,我正试图将它保存到NSUserDefaults.该NSMutableArray(strainsfinal)包含收藏项目列表.我希望此列表保存,即使应用程序关闭也会出现,然后重新启动.有谁知道为什么我的代码不工作(不保存我的数据)?见下文:
AppDelegate.m
-(void)updateStrains:(NSDictionary *)item
{
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataSave = [currentDefaults objectForKey:@"strains"];
if (strainsfinal != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];
if (oldSavedArray != nil)
strainsfinal = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
strainsfinal = [[NSMutableArray alloc] init];
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:Appdelegate.m所以我已经将上面的方法更改为下面的方法(我认为它应该工作),但应用程序崩溃,说:"由于未捕获的异常'NSInvalidArguementException'终止应用程序,原因:'NSConcreteAttributedString initwithString:零'价值'.
我错过了什么?!
-(void)updateStrains:(NSDictionary *)item {
NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:strainsfinal];
[[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"strains"];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"arrayupdated" object:self userInfo:nil];
NSLog(@"updated strains %@",strainsfinal);
}
Run Code Online (Sandbox Code Playgroud) 我试图使用下面的代码从我的 NSDictionary 中删除一个数组(从我的表视图中删除一个对象),但是由于某种原因我在这一行崩溃了:
[self.friendData removeObjectAtIndex:indexPath.row];
出现此错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSSingleObjectArrayI removeObjectAtIndex:]:无法识别的选择器发送到实例 0x1c0618e20”
知道这是为什么以及解决方法是什么吗?请参阅 self.friendData 的控制台信息和下面的代码:
视图控制器.h
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) IBOutlet UITableView *sidetableView;
@property (strong, nonatomic) NSMutableArray *myFriendData;
@property (weak, nonatomic) IBOutlet UITableView *neighboursView;
@property (strong, nonatomic) NSMutableArray *friendData;
@property (strong, nonatomic) NSMutableArray *neighbourData;
@property (strong, nonatomic) IBOutlet UITableView *friendsView;
Run Code Online (Sandbox Code Playgroud)
视图控制器.m
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.sidetableView) {
return UITableViewCellEditingStyleDelete;
}
if (tableView == self.friendsView) {
return UITableViewCellEditingStyleDelete;
}
else {
return UITableViewCellEditingStyleNone;
} …Run Code Online (Sandbox Code Playgroud)