小编gkl*_*lka的帖子

排序NSSet的最有效方法是什么?

NSSet/ NSMutableSet基于集合中对象的属性对对象进行排序的最有效方法是什么?现在,我这样做的方法是迭代每个对象,将它们添加到a中NSMutableArray,然后对该数组进行排序NSSortDescriptor.

sorting cocoa objective-c nsset data-structures

63
推荐指数
3
解决办法
4万
查看次数

为什么UITextField在resignFirstResponder上设置动画?

从iOS 8开始,表单中的UITextField表现得非常奇怪.如果我单击另一个文本字段或按键盘上的Tab键,输入的文本将向上设置动画,然后快速重新显示.每次视图加载后都会发生这种情况,之后不时发生.

它看起来像这样:

动画

我的代码看起来像这样:

#pragma mark - <UITextFieldDelegate>

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.passwordTextField) {
        [self loginButtonClicked:nil];
    } else if (textField == self.emailTextField) {
        [self.passwordTextField becomeFirstResponder];
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

看起来这个问题是由我的键盘监听器引起的:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];


- (void)keyboardWillHide:(NSNotification *)sender
{
    self.loginBoxBottomLayoutConstraint.constant = 0;

    [self.view layoutIfNeeded];
}

- (void)keyboardWillShow:(NSNotification *)sender
{
    CGRect frame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect newFrame = [self.view convertRect:frame fromView:[[UIApplication sharedApplication] delegate].window];
    self.loginBoxBottomLayoutConstraint.constant = CGRectGetHeight(newFrame);

    [self.view layoutIfNeeded];
}
Run Code Online (Sandbox Code Playgroud)

animation objective-c uitextfield ios resignfirstresponder

15
推荐指数
1
解决办法
995
查看次数

使用Autolayout iOS 6 +动态表格单元格高度

我有UITableviewCell子类.在那个单元格中,我有2个标签(lblCommentlblDateTimeStampe)和一个显示评级星的视图.我希望lblComment的动态高度适合所有文本.它应根据评论的长度在高度上扩展和缩小.我之前已经实现了这个,但没有像下面的AutoLayout

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {  

    NSString *label =  self.userComment.commentText;
    CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15]
                          constrainedToSize:CGSizeMake(320, 9999) 
                              lineBreakMode:UILineBreakModeWordWrap];

    return stringSize.height+10;

} 
Run Code Online (Sandbox Code Playgroud)

现在我正在使用AutoLayout功能.

如何使用Autolayout实现这一目标?

任何形式的帮助表示赞赏.谢谢

height dynamic uitableview ios6

10
推荐指数
1
解决办法
5294
查看次数

MKMapView的ContentInset

UIScrollView有一个很好的contentInset属性,告诉视图,哪个部分在屏幕上可见.我有MKMapView一个半透明视图部分覆盖的.我希望地图在视图下可见.我必须在地图上显示几个注释,并且我想使用它们缩放它们-setRegion:animated:,但是地图视图不尊重它被部分覆盖,因此我的一些注释将被半透明视图覆盖.

在此输入图像描述

有没有办法告诉地图,像滚动视图一样使用contentInset


更新:这是我尝试过的:

- (MKMapRect)mapRectForAnnotations
{
    if (self.trafik) {
        MKMapPoint point = MKMapPointForCoordinate(self.trafik.coordinate);
        MKMapPoint deltaPoint;

        if (self.map.userLocation &&
            self.map.userLocation.coordinate.longitude != 0) {
            MKCoordinateSpan delta = MKCoordinateSpanMake(fabsf(self.trafik.coordinate.latitude-self.map.userLocation.coordinate.latitude),
                                                          fabsf(self.trafik.coordinate.longitude-self.map.userLocation.coordinate.longitude));
            deltaPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(delta.latitudeDelta, delta.longitudeDelta));
        } else {
            deltaPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(0.01, 0.01));
        }

        return MKMapRectMake(point.x, point.y, deltaPoint.x, deltaPoint.y);
    } else {
        return MKMapRectNull;
    }
}
Run Code Online (Sandbox Code Playgroud)

objective-c mkmapview

9
推荐指数
2
解决办法
1328
查看次数

如何在iOS 10应用中为我的推送通知添加媒体附件?

有多个示例,您应该如何设置项目以添加使用"媒体附件"技术来显示图像的丰富通知.我已经阅读了大部分内容,但我错过了一些内容,因为我的项目没有显示任何带有此负载的丰富通知(使用APNS-Tool和Boodle进行测试):

{
    "aps":{
        "alert":{
            "title": "Rich test",
            "body": "Dancing Banana"
        },
        "mutable-content": 1
    }
}
Run Code Online (Sandbox Code Playgroud)

通知可见,但在3D Touch上,没有显示其他信息(如图像或修改后的标题).通知扩展断点和NSLog消息也不起作用.

这是我的演示项目:https://github.com/gklka/RichTest

我做了什么:

  1. 创建了一个新项目
  2. 实现iOS风格的授权:
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {

        if (granted) {
            NSLog(@"Granted");

            [[UIApplication sharedApplication] registerForRemoteNotifications];

        } else {
            NSLog(@"Error registering: %@", error);
        }

    }];
  1. 为AppDelegate添加了调试:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Token: %@", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error: %@", error);
}
  1. 为项目添加了新目标:通知服务扩展:

添加新目标

  1. 添加banana.gif …

objective-c push-notification apple-push-notifications ios10

9
推荐指数
1
解决办法
3430
查看次数

UIBarButtonItem大小不同

我有一个应用程序,它使用Storyboards显示两个屏幕.第一个是列表(在屏幕截图中称为"Lista"),第二个是地图("Térkép").每个视图都有一个左右导航按钮.按右键将地图视图推送到导航控制器.后退按钮是从地图的viewDidLoad方法手动隐藏的.

列表显示 地图视图

问题是,为什么每个屏幕上的条形按钮项目大小不同?如何控制按钮的大小?

按钮上的图像大小相同.AppDelegate通过外观设置完成蒙皮:

// navbar background
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg-titlebar.png"]
                                   forBarMetrics:UIBarMetricsDefault];

// navbar button background
[[UIBarButtonItem appearance] setBackgroundImage:[[UIImage imageNamed:@"btn-main.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(4.0, 4.0, 4.0, 4.0) resizingMode:UIImageResizingModeStretch]
                                        forState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:[[UIImage imageNamed:@"btn-main-active.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(4.0, 4.0, 4.0, 4.0) resizingMode:UIImageResizingModeStretch]
                                        forState:UIControlStateHighlighted
                                      barMetrics:UIBarMetricsDefault];

// back button background
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[[UIImage imageNamed:@"btn-back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(4.0, 12.0, 4.0, 4.0) resizingMode:UIImageResizingModeStretch]
                                                  forState:UIControlStateNormal
                                                barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[[UIImage imageNamed:@"btn-back-active.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(4.0, 12.0, 4.0, 4.0) resizingMode:UIImageResizingModeStretch]
                                                  forState:UIControlStateHighlighted
                                                barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

iphone uinavigationbar uibarbuttonitem ios

8
推荐指数
1
解决办法
514
查看次数

延迟推送通知并检查用户是否启用了它

我开发了一个应用程序,它围绕推送通知构建.仅当用户到达注册过程的某个点时,应用才会请求通知权限.我已经设法做了以下事情:

  • 该应用程序维护一个NSUserDefaults变量,指示是否需要在启动时注册推送(默认情况下:不)
  • 当注册到达那一点时,我翻转变量并-registerForRemoteNotificationTypes:在iOS 7和-registerUserNotificationSettings:iOS 8上调用

除非用户已启用推送通知,然后稍后在"设置"中禁用推送通知,否则此工作正常.在这种情况下,我尝试在启动时重新注册Push,它既不调用-application:didRegisterForRemoteNotificationsWithDeviceToken也不调用-application:didFailToRegisterForRemoteNotificationsWithError.

其他信息,iOS 8 -isRegisteredForRemoteNotifications也会返回YES.(我没有测试,但假设它-enabledNotificationTypes适用于iOS 7.)

如何检测此方案并向用户显示一个请求者,要求他在"设置"中重新启用通知?

objective-c push-notification apple-push-notifications ios ios8

8
推荐指数
1
解决办法
2437
查看次数

带块参数的NSInvocation

我正在尝试将块参数传递给a NSInvocation,但应用程序崩溃了.调用发出网络请求并调用成功或失败块.我认为问题是在网络请求完成之前会释放块.我设法让它与一些Block_copyhackery 一起工作,它没有使用Instruments报告任何泄漏.

问题: - 即使静态分析仪或仪器没有报告,泄漏是否可能存在? - 有没有更好的方法来"保留"该区块?

// Create the NSInvocation
NSMethodSignature *methodSignature = [target methodSignatureForSelector:selector];
NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:methodSignature];
[invoc setTarget:target];
[invoc setSelector:selector];

// Create success and error blocks.
void (^successBlock)(id successResponse) = ^(id successResponse) {
    // Some success code here ...
};

void (^errorBlock)(NSError *error) = ^(NSError *error) {
    // Some failure code here ...
};

/*
Without the two Block_copy lines, the block gets dealloced too soon
and the app crashes with …
Run Code Online (Sandbox Code Playgroud)

dealloc nsinvocation ios objective-c-blocks

7
推荐指数
1
解决办法
4012
查看次数

重新加载故事板以更改区域设置

我正在为我的应用程序使用单个本地化的Storyboard.该应用程序必须有一个选项来动态更改语言.到目前为止,我想出了如何更改应用程序启动期间使用的语言:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
Run Code Online (Sandbox Code Playgroud)

并设法通过删除关键窗口的所有子视图重新加载我的整个应用程序,从故事板重新加载初始视图控制器并使其成为新的根视图控制器.(它有一些问题,比如管理开放资源,但可以做到.)

我唯一想知道的是如何强制iOS以新语言重新加载故事板?似乎故事板以某种方式缓存,并且-UIStoryboad:stroyboardWithName:fromNib不会完全重新加载它,所以它出现在最后一种语言中.

我已经考虑了其他两个选择:

  • 每种语言都有不同的故事板文件(这是无法管理的,我不想这样做)

  • 在我的所有视图控制器中使用事件侦听器,它们响应语言更改并在UI上设置新字符串(每次从故事板中实例化任何接口元素时也必须调用它).这个解决方案需要更多的时间,而不是我们想要花费的时间,并且还大大增加了错误的可能性.

还有一些更聪明的方法吗?我不想保持我的应用程序的当前状态,所以重新启动它就像它被杀死和实现似乎是一个合理的解决方案.

localization ios uistoryboard

7
推荐指数
1
解决办法
1万
查看次数

在UINavigationController中获取有关不成功的后滑动手势的通知

我有一个UINavigationController子类,它管理一个自定义导航栏.此导航栏的外观取决于顶视图控制器.我向顶视图控制器询问何时-pushViewController:animated:-popViewControllerAnimated:发生的条形图类型.

问题是,当用户开始滑动时会播放导航栏类型更改,但我找不到任何告诉我滑动成功的事件,因此如果用户取消滑动,我会卡在前一个视图控制器上在导航堆栈中使用所需的导航栏外观.

我试过了UINavigationControllerDelegate,但既没有-navigationController:didShowViewController:animated:也没有-navigationController:didShowViewController:animated:被召唤.我的第二个想法是使用interactiveGestureRecognizer,但似乎它在成功和取消后滑动时都成功结束,并且在topViewController调用识别器事件时也是如此.

我知道,顶视图控制器-viewDidAppear将在取消滑动时再次调用,但我不希望我的用户在其控制器中实现任何逻辑以支持我的导航实现.

有任何想法吗?

objective-c uinavigationcontroller uigesturerecognizer

7
推荐指数
1
解决办法
2555
查看次数