我遇到了一个问题,我无法理解为我的生活.我搜索了互联网,试图了解Swifts的EXC_BAD_ACCESS,但似乎没有任何帮助.
以下代码很长,但大多数时候注释都是理解相关项所需的所有信息.
我有一个类CalculatorController,其中包含以下相关方法和属性:
import UIKit
class CalculatorController: UIViewController {
// the actual `@IBOutlet` which is never accessed directly
@IBOutlet private weak var _mainDisplay: UILabel!
// an instance of `MainDisplayMicroController`
// holds a reference to `_mainDisplay`
// is used to manipulate `_mainDisplay` in a controlled way
private var mainDisplay: MainDisplayMicroController!
override func viewDidLoad() {
super.viewDidLoad()
// connects `mainDisplay` with `_mainDisplay`
mainDisplay = MainDisplayMicroController(label: _mainDisplay)
// sets `_mainDisplay`'s `text` property to "0"
mainDisplay.content = .Number(0)
//...
}
//... …Run Code Online (Sandbox Code Playgroud) 我一直在审查Apple文档和示例代码,以尝试确定管理IBOutlet内存的最佳方法.至少可以说,我有点困惑.
CurrentAddress示例代码将IBOutlets声明为属性:
@interface MapViewController : UIViewController <MKMapViewDelegate, MKReverseGeocoderDelegate>
{
MKMapView *mapView;
UIBarButtonItem *getAddressButton;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *getAddressButton;
Run Code Online (Sandbox Code Playgroud)
大.这些是在dealloc中发布的:
- (void)dealloc
{
[mapView release];
[getAddressButton release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
现在不应该将这些属性设置为分配?因为当设置为retain时,IBOutlet的保留计数将增加两次:一次加载nib时和另一次设置属性时?将这些属性设置为nil而不是在dealloc中释放不是更好吗?
我想用代码而不是界面构建器创建IBOutlets和IBActions.
假设我的UI上有一个按钮btnDisplay,我的代码中有一个方法叫做displayMessage.我需要写的代码是什么,以便在btnDisplay点击时displayMessage运行?
我可能正在做一些非常愚蠢的事情,但我似乎无法使用Interface Builder将IBOutlet变量连接到自定义视图,但仅限于Swift.
我创建了一个名为MyView的类,它从UIView扩展而来.在我的控制器中,我有一个MyView变量(声明为@IBOutlet var newView:MyView).我进入IB并将UIView拖到窗口上并给它一类MyView.
每当我在Objective C中完成类似操作时,我就可以单击应用程序窗口顶部的View Controller按钮,选择变量并将其向下拖动到控件以将两者链接在一起.当我在Swift中尝试它时,它拒绝承认视图在那里.
如果我将控制器中变量的类更改为UIView,它可以正常工作.但不是我的自定义视图.
还有其他人遇到过这个问题吗?它是一个功能,还是只是我的愚蠢?
控制器代码
import UIKit
class ViewController: UIViewController {
@IBOutlet var newView:MyView
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Run Code Online (Sandbox Code Playgroud)
视图代码
import UIKit
class MyView: UIView {
init(frame: CGRect) {
super.init(frame: frame)
// Initialization code
}
/*
// Only override drawRect: if you perform custom …Run Code Online (Sandbox Code Playgroud) 我在代码中定义了这个:
@property (nonatomic, weak) IBOutletCollection(UITableViewCell) NSSet * certaintyCells;
Run Code Online (Sandbox Code Playgroud)
并合成.我绝对确定这个控制器用在故事板上,并将三个单元连接到这个集合.
接下来,在didSelectRowAtIndexPath:方法调用中,我添加了此代码,并添加了NSLog用于调试:
NSLog(@"Certainty Cells: %@",certaintyCells);
for (UITableViewCell * cell in certaintyCells) {
[cell.textLabel setTextColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1]];
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
}
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
Certainty Cells: (null)
Run Code Online (Sandbox Code Playgroud)
当然,预期的行为不会发生.
关于为什么会发生这种情况的任何想法?我确实确保使用的是静态单元,而不是动态原型.作为旁注,这三个单元也连接到(工作)它们自己的IBOutlet.
谢谢,
使用Xcode 4.3.3,我无法弄清楚如何将自定义UIView类中的出口与Interface Builder中创建的对象连接起来.
在一个ViewController中,我有各种按钮,滑块等,我正在尝试将其分组到Views中.因此,在IB的ViewController中,我添加了3个视图.在任何给定时间只能看到1个视图.
我派生了自定义UIView类来处理这3个视图中的每一个.我的视图控制器实例化每个类.我在IB中选择了视图,打开了Identity Inspector并将Class设置为我的自定义类.但是当我尝试将视图中的连接和/或它的组成控件拖动到自定义视图的.h文件时,IB将不会添加连接.
IB允许我通过拖动到父视图控制器的.h来添加出口/操作,但不能添加到自定义视图的.h文件.我想过,一旦我将View的类设置为我的自定义类,我就可以将视图组件的出口拖到我的自定义类而不是视图控制器中.
这个问题与我的相似:如何将UIview出口连接到自定义子视图这两个解决方案(手动添加出口并在IB中设置视图的类)并没有改变我的行为.这是我添加的手动插座:
customView3.h
#import <UIKit/UIKit.h>
@interface customView3 : UIView
@property (retain) IBOutlet customView3 *view3;
@property (retain) IBOutlet UISlider *slider;
@end
Run Code Online (Sandbox Code Playgroud)
customView3.m
#import "customView3.h"
@implementation customView3
@synthesize view3, slider;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?为了向我的自定义UIView而不是视图控制器添加插座,我还需要设置/检查什么?
DetailViewController:
@IBOutlet var selectedBundesland: UILabel!
Run Code Online (Sandbox Code Playgroud)
TableViewController:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "BackToCalculator") {
var vc:FirstViewController = segue.destinationViewController as FirstViewController
vc.selectedBundesland.text = "Test"
}
Run Code Online (Sandbox Code Playgroud)
IBOutlet已连接!
错误:致命错误:在解包可选值时意外发现nil
我阅读了关于Optionals的多个页面,但我不知道我的问题的答案.
您是否需要有关我的项目的更多信息?
我想NSLocalizedString(key, comment)从故事板分配到UILabel而不为它创建一个插座.
我的应用程序的RootViewController有问题.当我运行应用程序时,我收到此错误.这意味着什么?
NSUnknownKeyException', reason: '[<UIApplication 0x8634010> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bigImageView.'
First throw call stack:
(0x1992012 0x1357e7e 0x1a1afb1 0xe04711 0xd85ec8 0xd859b7 0xdb0428 0x4bc0cc
0x136b663 0x198d45a 0x4babcf 0x4bc98d 0x29eceb 0x29f002 0x29ded6 0x2af315 0x2b024b
0x2a1cf8 0x1dbedf9 0x1dbead0 0x1907bf5 0x1907962 0x1938bb6 0x1937f44 0x1937e1b 0x29d7da 0x29f65c 0x2c6d 0x2b95)
libc++abi.dylib: terminate called throwing an exception
Run Code Online (Sandbox Code Playgroud)
我在RootViewController.h中的代码
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController <UIGestureRecognizerDelegate>{
}
@property (nonatomic, copy)IBOutlet UIImageView * bigImageView;
@property (nonatomic, assign)BOOL fromRootViewController;
- (void)handleTap:(UITapGestureRecognizer *)recognizer;
@end
Run Code Online (Sandbox Code Playgroud)
我在RootViewController.m中的代码 …
我将一个IBOutlet和IBAction我的按钮变量从Interface Builder连接到我的View Controller.如何在Swift中为按钮添加动作方法?这段代码似乎不起作用.
@IBOutlet var OK: UIButton!
@IBAction func OK(sender: UIButton){}
Run Code Online (Sandbox Code Playgroud)
我找到的Objective-C等价物是:
@interface Controller
{
IBOutlet id textField; // links to TextField UI object
}
- (IBAction)doAction:(id)sender; // e.g. called when button pushed
Run Code Online (Sandbox Code Playgroud) iboutlet ×10
ios ×7
swift ×4
iphone ×3
uilabel ×2
cocoa-touch ×1
exception ×1
ibaction ×1
ipad ×1
objective-c ×1
segue ×1
storyboard ×1
tableview ×1
uistoryboard ×1
uiview ×1
xcode ×1