小编rde*_*mar的帖子

activateConstraints:和deactivateConstraints:在IB中创建的约束旋转后不持久

新的NSLayoutConstraint方法activateConstraints:deactivateConstraints:不会出现与IB-创建的约束正常工作(他们正常工作的代码创建的约束).我用一个按钮创建了一个简单的测试应用程序,该按钮有两组约束.安装的一组具有centerX和centerY约束,而另一组(已卸载)具有顶部和左侧约束(常数10).按钮方法切换这些约束集.这是代码,

@interface ViewController ()
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *uninstalledConstraints;
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *installedConstraints;
@end

@implementation ViewController


- (IBAction)switchconstraints:(UIButton *)sender {
    [NSLayoutConstraint deactivateConstraints:self.installedConstraints];
    [NSLayoutConstraint activateConstraints:self.uninstalledConstraints];
}


-(void)viewWillLayoutSubviews {
    NSLog(@"installed: %@    uninstalled: %@", ((NSLayoutConstraint *)self.installedConstraints[0]).active ? @"Active" : @"Inactive", ((NSLayoutConstraint *)self.uninstalledConstraints[0]).active ? @"Active" : @"Inactive");

}
Run Code Online (Sandbox Code Playgroud)

当应用程序启动时,按钮处于由其安装的约束定义的正确的居中位置.在按钮的操作方法中执行激活/取消激活后,按钮会正确移动到新位置,但是当我将视图旋转到横向时,它会移回到最初定义的位置(尽管日志仍然显示新激活的设置为活跃).当我旋转回肖像时,按钮保持在其初始位置(在屏幕中央),现在日志显示初始约束集合处于活动状态,而我激活的约束处于非活动状态.

问题是,这是一个错误,还是这些方法不应该以这种方式使用IB定义的约束?

cocoa-touch ios autolayout nslayoutconstraint

27
推荐指数
2
解决办法
9412
查看次数

在基于视图的表中获取按钮的行

当您单击按钮时,如何获取基于视图的表中的按钮行?单击按钮时未选中该行,但我发现如果您在按钮的操作方法中记录sender.superview.superview,我会得到:NSTableRowView:0x1001b3a90 - row:2.因此,该行在日志中,但我不知道如何以编程方式获得它.我在NSTableCellView的子类中有按钮的动作方法.

cocoa objective-c nstableview

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

动画期间未正确定位子视图

我有一个简单的动画,我正在从旧的中心扩展一个新视图,而那个视图淡出.但是,当新视图扩展为占据整个屏幕时,此新视图(按钮和标签)的子视图从屏幕右下方"飞入".我已尝试使用和不使用autolayout,虽然这两种情况给出了不同的结果,但它们都是错误的.

设置很简单,我在故事板中有两个未连接的视图控制器,并使用以下代码为视图更改设置动画:

-(void)switchViews2:(id)sender {
    UIWindow *win = self.view.window;
    YellowController *yellow = [self.storyboard instantiateViewControllerWithIdentifier:@"Yellow"];
    yellow.view.frame = CGRectMake(0, 0, 1, 1);
    yellow.view.center = self.view.center;
    [win addSubview:yellow.view];
    CGRect  frame = self.view.frame;
    [UIView animateWithDuration:5 animations:^{
        yellow.view.frame = frame;
        self.view.alpha = 0;
    }
             completion:^(BOOL finished) {
                 [self.view removeFromSuperview];
                 win.rootViewController = yellow;
         }];
}
Run Code Online (Sandbox Code Playgroud)

问题是,为什么子视图不会停留在超级视图内的位置,因为它会动画.

uiviewanimation ios

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

如何在Swift中使用indicesOfObjectsPassingTest:

indexOfObjectsPassingTest的声明:在Swift中看起来像这样

func indexesOfObjectsPassingTest(predicate: ((AnyObject!, Int, CMutablePointer<ObjCBool>) -> Bool)!) -> NSIndexSet!
Run Code Online (Sandbox Code Playgroud)

我已经尝试了各种各样的排列来让它发挥作用,但我很难过,特别是你如何处理这件作品,CMutablePointer<ObjCBool>) -> Bool)!.我发现它很混乱,当我第一次在objective-c中学习块时,你如何从声明转换为实际使用方法,而Swift的语法至少是令人困惑的.

closures ios swift

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

在自定义容器控制器中旋转后未调整大小的视图

我已经制作了一个测试应用程序来熟悉自定义容器视图控制器.如果我在应用程序首次启动时或切换到其他视图控制器后旋转设备,则新视图会调整大小以占用整个屏幕,正如我所预期的那样.但是,如果我在应用程序启动后旋转,然后切换到新的视图控制器,视图将保持其纵向大小而不是越来越短(实际上它略有不同 - 从320,460到300,480).主视图控制器在app delegate(no xib)中为init init,并设置为窗口的根视图控制器.这是我在MasterViewController(自定义容器控制器)中的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    WelcomeController *welcome = [[WelcomeController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.currentController = welcome;
    [self addChildViewController:welcome];
    [self.view addSubview:welcome.view];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];
}


- (void)swipeLeft:(UISwipeGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateRecognized) {
        UIActionSheet *sheet =[[UIActionSheet alloc] initWithTitle:@"Select A Destination" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"welcome",@"Play",@"Scores", nil];
        [sheet showInView:self.view];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:{
            if ([self.currentController class] != [WelcomeController class] ) …
Run Code Online (Sandbox Code Playgroud)

uiviewcontroller ios

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

NSPopupButton绑定与值变换器

我不知道我用弹出式按钮填充的值是否与应变值绑定是否应该是它应该是不是 - 这是我看到的不寻常的事情(至少就我所看到的而言)值转换器和表视图)是transformedValue:方法中的"value"参数是绑定到数组控制器的整个数组,而不是数组中的单个字符串.当我使用表视图完成此操作时,变换器将为表中的每个显示行调用一次,"value"参数是绑定到该行和列的任何对象,而不是用作内容数组的整个数组对于阵列控制器.

我有一个非常简单的应用程序来测试它.在app委托中有这样的:

+(void)initialize {
    RDTransformer *transformer = [[RDTransformer alloc] init];
    [NSValueTransformer setValueTransformer:transformer forName:@"testTransformer"];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    self.theData = @[@{@"name":@"William", @"age":@"24"},@{@"name":@"Thomas", @"age":@"23"},@{@"name":@"Alexander", @"age":@"64"},@{@"name":@"James", @"age":@"47"}];
}
Run Code Online (Sandbox Code Playgroud)

在RDTransformer类中是这样的:

+ (Class)transformedValueClass {
    return [NSString class];
}


+(BOOL)allowsReverseTransformation {
    return NO;
}

-(id)transformedValue:(id)value {
    NSLog(@"%@",value);
    return value;
}
Run Code Online (Sandbox Code Playgroud)

在IB中,我向窗口添加了一个NSPopupButton,并在对象列表中添加了一个数组控制器.控制器的内容数组绑定到App Delegate.theData,弹出按钮的内容值绑定到Array Controller.arrangedObjects.name,其值为transform,testTransformer.

当我运行程序时,来自transformedValue:方法的日志是这样的:

2012-09-19 20:31:39.975 PopupBindingWithTransformer[793:303] (
)
2012-09-19 20:31:40.019 PopupBindingWithTransformer[793:303] (
    William,
    Thomas,
    Alexander,
    James
)
Run Code Online (Sandbox Code Playgroud)

从我在SO上可以看到的情况来看,这似乎不是其他人的经验.对于绑定或值变换器,我有什么问题吗?

macos objective-c cocoa-bindings nspopupbutton nsvaluetransformer

4
推荐指数
1
解决办法
1213
查看次数