Mac OS app 用 StoryBoard开发的一点感想

时间:2020-05-25 19:51:37   收藏:0   阅读:126

Mac OS app 用 StoryBoard开发的一点感想。

前提

最近在学习app的逆向,准备自己搞一个app,然后用逆向工具对其进行恶搞一下。

选择

纠结在于用Mac app还是 IOS app,纠结了一下,选择了Mac app。
原因:

趟坑

app的功能

遇到问题

  1. 代码里如何或者输入框里的内容
    在ViewController.h中绑定两个属性,然后在impl中可以直接使用
    做法:还是分屏,然后ctrl拖拽,之后就会生成两个属性
// interface
#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController
@property (weak) IBOutlet NSTextField *nameTextField;
@property (weak) IBOutlet NSTextField *phoneTextField;
@end

//impl
#import "ViewController.h"

@implementation ViewController

@synthesize nameTextField;
@synthesize phoneTextField;

// 获取姓名输入框的内容
- (NSString *)getName {
    return [nameTextField stringValue];
}
// 获取手机号输入框的内容
- (NSString *)getPhone {
    return [phoneTextField stringValue];
}
//清除输入框内容
- (IBAction)CancelClicked:(id)sender {
    [nameTextField setStringValue:@""];
    [phoneTextField setStringValue:@""];
}
// other code
@end

  1. 如何设置背景色
    拓展NSView,添加设置背景色方法,然后绑定到UI层,
    • 实现代码
// interface
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

@interface NSView (backGroundColor)
-(void) setBackGroundColor: (NSColor *) color;
@end

//impl
#import "backgroundColor.h"

@implementation NSView (backgroundColor)
- (void)setBackGroundColor:(NSColor *)color {
    [self setWantsLayer:YES];
    self.layer = [CALayer layer];
    [self.layer setBackgroundColor:[color CGColor]];
}
@end

技术图片

  1. 如何点击确定跳转到另一个界面呢
    在storyboard界面找到按钮,ctrl拖到它点击之后要跳转的界面

技术图片

之后出现这个dialog
技术图片

这样就完成了。

  1. 如何将值传过去呢
    先看一下两个界面之间建立了啥
    技术图片

是个NSStoryboardSegue类型的东西

@interface NSStoryboardSegue : NSObject

/* NSStoryboardSegue instances have optional identifiers that can be assigned in Interface Builder. These identifiers can be used in overrides of -[NSViewController prepareForSegue:sender:] to differentiate segues. */
@property (nullable, readonly, copy) NSStoryboardSegueIdentifier identifier;

/* Subclasses of NSStoryboardSegue can use this property to access the source view or window controller that is being segued away from. */
@property (readonly, strong) id sourceController;

/* Subclasses of NSStoryboardSegue can use this property to access the destination view or window controller that‘s being segued to. This property is also essential for overrides of -[NSViewController prepareForSegue:sender:], which is passed to the source view controller. This property allows the receiver of -[NSViewController prepareForSegue:sender:] to access and pass configuration data to the destination controller. */
@property (readonly, strong) id destinationController;

@end

有sourceController和destinationController,是不是很明确了

```objective-c
  - (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(nullable id)sender {
        DetailController *detailController = [[DetailController alloc] init]; // destinationController
        detailController = segue.destinationController;
        UserInfo *userInfo = [[UserInfo alloc] init];
        [userInfo setName:[self getName] andPhone:[self getPhone]];
        detailController.userInfo= userInfo;//设置要传递的值
    }  
```
2. destinationController
添加一个属性接收传递的值,在`viewDidLoad`方法里将传递的值给赋值到view上
```objective-c
// interface
    @interface DetailController : NSViewController
    @property UserInfo *userInfo;
    @property (weak) IBOutlet NSTextField *nameTextField;
    @property (weak) IBOutlet NSTextField *phoneTextField;
    @end


// impl
    @implementation DetailController
    @synthesize userInfo;
    @synthesize nameTextField;
    @synthesize phoneTextField;

    - (void)viewDidLoad {
        [super viewDidLoad];
        [nameTextField setStringValue:[userInfo name]];
        [phoneTextField setStringValue:[userInfo phone]];
    }
    @end
```

结束

技术图片

遇到错误

resource fork, Finder information, or similar detritus not allowed
Command CodeSign failed with a nonzero exit code

解决方法:进入项目的根目录执行 xattr -cr .

引用

源码

https://github.com/1483523635/simple-MacOS-app

博客原始地址

https://github.com/1483523635/blogs/blob/master/MacOSApp/StoryBoard.md

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!