小编Ash*_*row的帖子

使用子类化在NSSecureTextField中垂直居中文本

我正试图在我的NSTextFields中垂直居中文本,但其中一个是密码,所以它是一个NSSecureTextField.我已经将它的类设置MDVerticallyCenteredSecureTextFieldCell为以下实现:

- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame {
    // super would normally draw text at the top of the cell
    NSInteger offset = floor((NSHeight(frame) - 
                              ([[self font] ascender] - [[self font] descender])) / 2);
    return NSInsetRect(frame, 0.0, offset+10);
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
               editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event {
    [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
                  inView:controlView editor:editor delegate:delegate event:event];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
                 editor:(NSText *)editor delegate:(id)delegate 
                  start:(NSInteger)start length:(NSInteger)length {

    [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
                    inView:controlView editor:editor delegate:delegate
                     start:start length:length];
}

- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
    [super …
Run Code Online (Sandbox Code Playgroud)

macos objective-c nstextfield

11
推荐指数
2
解决办法
2086
查看次数

dealloc在使用ARC构建的后台GCD队列崩溃应用程序上调用

我有一个视图控制器,可以在后台GCD队列中下载资产.我将下载函数传递给回调块,以便在下载完成后执行,并且它总是在主线程上执行此块.

如果我的视图控制器在下载完成之前被用户解雇,则会出现此问题.我怀疑发生了什么,一旦我的视图控制器被解除,回调块是唯一保留对控制器的强引用的东西.回调块仅保留在后台线程中,因此一旦释放,回调块范围内捕获的所有对象也将被释放,尽管在后台队列中.

这就是问题:在后台队列中释放会导致dealloc在同一队列中运行,而不是在主队列中运行.反过来,这会dealloc在后台调用,应用程序崩溃:

2012-01-19 12:47:36.349 500px iOS[4892:12107] bool _WebTryThreadLock(bool), 0x306c10: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
[Switching to process 16643 thread 0x4103]
[Switching to process 16643 thread 0x4103]
(gdb) where
#0  0x307fd3c8 in _WebTryThreadLock ()
#1  0x307ff1b0 in WebThreadLock ()
#2  0x33f7865e in -[UITextView dealloc] ()
#3  0x0005d2ce in -[GCPlaceholderTextView dealloc] …
Run Code Online (Sandbox Code Playgroud)

memory-management objective-c grand-central-dispatch ios automatic-ref-counting

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

ARC中的ivar块中的__block自引用循环

我在块ivar中得到了一些具有明显参考周期的代码.以下代码导致引用循环,并且永远不会调用dealloc:

__block MyViewController *blockSelf = self;

loggedInCallback = ^(BOOL success, NSError *error){
    if (success)
    {
        double delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
        {
            [blockSelf.delegate loginDidFinish];
        });            
    }
};
Run Code Online (Sandbox Code Playgroud)

但是,如果我创建另一个__block变量来保存对我的委托的引用以捕获块的范围,则引用周期消失:

__block id <MyViewControllerDelegate> blockDelegate = self.delegate;

loggedInCallback = ^(BOOL success, NSError *error){
    if (success)
    {
        double delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
        {
            [blockDelegate loginDidFinish];
        });            
    }
};
Run Code Online (Sandbox Code Playgroud)

只是想了解这里发生了什么.

memory-management objective-c grand-central-dispatch objective-c-blocks automatic-ref-counting

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

多个装饰视图添加到UICollectionView

我正在创建一个子类的基本集合视图布局UICollectionViewFlowLayout.但是,我注意到似乎有几个装饰视图堆叠在一起.

每当用户选择最后一节中的项目时,我将添加一个包含以下代码的新部分.似乎每当我执行此代码时,都会在每个已存在的部分中添加一个装饰视图的附加副本.

[collectionView performBatchUpdates:^{
    currentModelArrayIndex++;
    [collectionView insertSections:[NSIndexSet indexSetWithIndex:currentModelArrayIndex]];
    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:currentModelArrayIndex-1]];
} completion:^(BOOL finished) {
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:currentModelArrayIndex] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}];
Run Code Online (Sandbox Code Playgroud)

我已经给我的装修意见证实了这一alpha0.2f,看到它们叠加起来.

在此输入图像描述

我还执行了集合视图层次结构的转储,看到10个实例,AFDecorationView我应该只看到4:

   | <AFDecorationView: 0x719ee50; baseClass = UICollectionReusableView; frame = (0 65; 768 208.125); alpha = 0; hidden = YES; layer = <CALayer: 0x719eec0>>
   | <AFDecorationView: 0x71ad980; baseClass = UICollectionReusableView; frame = (0 333.125; 768 203.281); alpha = 0; hidden = YES; layer = <CALayer: 0x71adb60>>
   | <AFDecorationView: 0x71afc90; baseClass …
Run Code Online (Sandbox Code Playgroud)

objective-c ios ios6 uicollectionview uicollectionviewlayout

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

如何在iPhone应用程序中设置uitextfield bordercolor

UITextView当我们在textfield和textview中输入或编辑时,如何以编程方式设置uitextfield和边框颜色.

我使用了这段代码,但没有更改边框颜色UITextView.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
Run Code Online (Sandbox Code Playgroud)

iphone objective-c ios

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

如何将多个UIBarButtonItem添加到UINavigationBar?

我想许多添加UIBarButtonItem的到UINavigationbar,不只是左,右按键:

logoButton = [[UIBarButtonItem alloc] initWithTitle:@"A Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonAClicked:)];

logoButton2 = [[UIBarButtonItem alloc] initWithTitle:@"B Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonBClicked:)];

logoButto3 = [[UIBarButtonItem alloc] initWithTitle:@"C Button" style:UIBarButtonItemStyleBordered target:self action:@selector(logoButtonCClicked:)];

self.navigationController.navigationBarHidden = NO;

self.title = @"Title";

NSArray* items = [[NSArray alloc] initWithObjects:logoButtonA, logoButtonB, logoButtonC, nil];
self.navigationController.navigationBar.items = items;
Run Code Online (Sandbox Code Playgroud)

我收到了SIGBRTself.navigationController.navigationBar.items = items;

如何将多个UIBarButtonItems 添加到UINavigationBar

cocoa-touch objective-c uinavigationbar uibarbuttonitem ipad

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

将RAC命令与异步网络操作一起使用

我正在使用UAGitHubEngineGitHub的API.我想写一个功能性的被动应用程序来获取一些数据.我依靠这里的代码来设置异步网络请求.我正在寻找的是一些名为"General"的团队的团队ID.我可以做过滤/打印部分OK:

[[self.gitHubSignal filter:^BOOL(NSDictionary *team) {
    NSString *teamName = [team valueForKey:@"name"];
    return [teamName isEqualToString:@"General"];
}] subscribeNext:^(NSDictionary *team) {

    NSInteger teamID = [[team valueForKey:@"id"] intValue];

    NSLog(@"Team ID: %lu", teamID);
}];
Run Code Online (Sandbox Code Playgroud)

但是设置命令对我来说是一个谜:

self.gitHubCommand = [RACCommand command];

self.gitHubSignal = [self.gitHubCommand addSignalBlock:^RACSignal *(id value) {
    RACSignal *signal = ???

    return signal;
}];
Run Code Online (Sandbox Code Playgroud)

当某些异步网络调用返回时,如何设置信号块以返回推送事件的信号?

objective-c ios reactive-cocoa raccommand

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

菜单栏应用程序永远不会重新激活

我正在构建一个Mac应用程序,它只位于菜单栏中,没有停靠项,没有关键窗口,也没有主菜单(它LSUIElement位于info.plist中YES).当我第一次启动应用程序时applicationDidBecomeActive:,就像我预期的那样被调用.然而,一旦另一个应用程序获得焦点,applicationDidBecomeActive:永远不会再次调用.

这可以防止我的应用程序中的文本字段成为第一个响应者.当我第一次打开应用程序时,文本字段是可编辑的:

在另一个应用获得焦点之前

但是在另一个应用程序到达前台后,文本字段不可编辑:

在另一个应用获得焦点后

我尝试过的:

打开菜单时,menuWillOpen:会在代理上调用NSMenu.我试过放下以下内容但没有成功:

[NSApp unhide];
[NSApp arrangeInFront:self];
[NSApp activateIgnoringOtherApps:YES];
[NSApp requestUserAttention:NSCriticalRequest];
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
[[NSRunningApplication currentApplication] unhide];
Run Code Online (Sandbox Code Playgroud)

我认为问题可能与没有任何窗户带到前面有关.我觉得我在这里抓住稻草.任何帮助将不胜感激.

macos objective-c menubar appkit

6
推荐指数
1
解决办法
872
查看次数

在iOS 5上的MPMusicPlayerController上淡出播放音量

我正在使用这个答案来淡出我的应用程序中的音乐播放器音量,但是在iOS 5中,这导致屏幕上的HUD音量显示给用户,好像他们按下了他们侧面的音量按钮一样设备.有谁知道不显示HUD的解决方法?

在此输入图像描述

objective-c mpmusicplayercontroller ios ios5

5
推荐指数
1
解决办法
4472
查看次数

如何将ReactiveCocoa与手势识别器一起使用

我正在使用ReactiveCocoa构建应用程序.顶视图是一个菜单,可以向下拉,然后向上推.我必须使用两种不同的手势识别器 - 一种用于拉下,另一种用于推回.一次只能启用一个 - 这就是我的问题.州.

我正在使用BlocksKit扩展来设置手势识别器.

self.panHeaderDownGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
    UIPanGestureRecognizer *recognizer = (UIPanGestureRecognizer *)sender;

    CGPoint translation = [recognizer translationInView:self.view];

    if (state == UIGestureRecognizerStateChanged)
    {
        [self.downwardHeaderPanSubject sendNext:@(translation.y)];
    }
    else if (state == UIGestureRecognizerStateEnded)
    {
        // Determine the direction the finger is moving and ensure if it was moving down, that it exceeds the minimum threshold for opening the menu.
        BOOL movingDown = ([recognizer velocityInView:self.view].y > 0 && translation.y > kMoveDownThreshold);

        // Animate the …
Run Code Online (Sandbox Code Playgroud)

objective-c reactive-programming ios reactive-cocoa

5
推荐指数
1
解决办法
3444
查看次数