我正在使用[[NSBundle mainBundle] loadNibNamed:...]从nib加载UITableViewCell.现在我想在我的代码中使用tableviewcell之前以编程方式进行一些后初始化工作.我应该把这段代码放在哪里,因为我似乎无法在initWithCoder方法中执行此操作,因为类中的标签对象仍为nil(因此无法设置任何内容).tableviewcell中的UILabel何时初始化(它们都被定义为IBOutlets)?
我在Xcode4 Interface Builder中编辑xib文件,按钮看起来没问题,但标签缺少抗锯齿.
当我创建一个新文件并添加标签时,它们看起来很好.有任何想法吗?
这是截图(无法发布图片,抱歉)http://i.imgur.com/DzFGy.png
我正在使用各种按钮,我已经从Xib文件设置了它的标签.并将所有按钮连接到单个方法-(void) note:(id)sender.
现在我想要检索标签号.所以我可以看到点击了哪个按钮
-(void) note:(id)sender
{
NotesClass *note = [[NotesClass alloc] initWithNibName:@"NotesClass" bundle:nil];
note.notetag = sender;
NSLog(@"%@",note.notetag);
[self.navigationController pushViewController:note animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
打印那个NSlog我得到这个输出:
<UIButton: 0x4e70350; frame = (227 119; 20 18); opaque = NO; autoresize = RM+BM; tag = 1; layer = <CALayer: 0x4e70480>>
Run Code Online (Sandbox Code Playgroud)
任何人都可以请帮助我.
怎么做?我只是想加载一个窗口并将其显示在主窗口的前面.
NSWindowController* controller = [[NSWindowController alloc] initWithWindowNibName: @"MyWindow"];
NSWindow* myWindow = [controller window];
[myWindow makeKeyAndOrderFront: nil];
Run Code Online (Sandbox Code Playgroud)
此代码显示窗口一会儿然后隐藏它.恕我直言这是因为我不保持对窗口的引用(我使用ARC).[NSApp runModalForWindow: myWindow];效果很好,但我不需要以模态显示它.
在ios应用程序中,我有一个UITableView.在cellForRowAtIndexPath方法中,我需要使用它的NIB名称返回自定义单元格.为此,我使用loadNibNamed.(我将在'willDisplayCellforRowAtIndexPath'中加载后填充单元格中的数据)
MyItemCell是一个XIB文件(MyItemCell.xib),包含2个UIImageView和一个UIButton(每个项目都有一个标签)
这是我的代码:
在我的viewController中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [ViewHelper loadCustomCellWithNibName:@"MyItemCell" owner:self];
}
Run Code Online (Sandbox Code Playgroud)
以及从NIB加载Custom单元格的方法
+ (UITableViewCell *) loadCustomCellFromNib:(NSString *)nibName owner:(id)owner
{
UITableViewCell *cell = nil;
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil];
if([nibObjects count] > 0 )
{
cell = [nibObjects objectAtIndex:0];
}
else
{
NSLog(@"Failed to load %@ XIB file!", nibName);
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
在所有测试中一切正常.但是我收到了一些我无法重现的用户崩溃.
这是崩溃:
NSInternalInconsistencyException
Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/7A24cE79-131F-523F-4C00-23B523ARG123/MyApp.app> (loaded)' with name 'MyItemCell'
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪: …
这就是我所做的.
我去了appDelegate.h文件并做了
` - @ Property(assign)IBOutlet NSViewController*popVC;
然后我去了applicationDidFinishLaunching:方法并做了
popVC = [[NSViewController alloc] init];
结果:我收到以下错误消息:

笔尖上的对象是否应该是弱的,因为它已经被笔尖拥有了?
首先,我检查了有关此主题的所有其他问题:提供的答案似乎是:
我的代码被编译为静态库,我导出.a文件和xib文件.我使用它的示例应用程序包括其包中的xib(构建阶段的"复制包资源")
在我的库代码中,我在一个单独的UIViewContoller子类中有一个函数来从nib创建视图控制器:
- (void) presentCustomController
{
self.vCtrl = [[CustomController alloc] initWithNibName:@"CustomController" bundle:nil];
...
}
Run Code Online (Sandbox Code Playgroud)
当我运行示例应用程序时,我检查self.vCtrl的_view成员,它是0x0000,当然这是导致发布标题中的异常的原因.我的理解是,该成员指向的视图是从xib文件"自动生成"的,而它的子项是我放入其中的控件(按钮等).我的理解中有哪些部分已经失败了?该问题是否与静态库中的事实有关?谢谢你的帮助.
作为ios编程的新手,
我开始使用故事板,然后我读到当多个开发人员在同一个应用程序上工作时它有其局限性,所以我切换到纯代码.但我猜xib/nib可能是一个很好的妥协.
你能给我一个更成熟的观点吗?
非常感谢您的灯光
我正在尝试使用xib文件显示自定义更改视图。为此,我从这里使用SimpleAlert 。我的.xib文件中包含textFields,作为我设置的.xib所有者,创建了ViewController。
import UIKit
class TestBaumViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var tfFirst: UITextField!
@IBOutlet weak var tfSecond: UITextField!
@IBOutlet weak var tfThird: UITextField!
@IBOutlet weak var tfFourth: UITextField!
init() {
super.init(nibName: "TestBaumViewController", bundle: nil)
initializeTextFields()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//initializeTextFields()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
//initializeTextFields()
}
override func awakeFromNib() {
super.awakeFromNib()
//initializeTextFields()
}
override func …Run Code Online (Sandbox Code Playgroud) 目前以相同的nibname加载viewcontroller我使用代码如下
let recommendationVC : RecommendationVC = RecommendationVC(nibName: "RecommendationVC", bundle: nil)
Run Code Online (Sandbox Code Playgroud)
我感觉不需要指定nibname,因为它与控制器名称相同。所以我决定使用泛型,并使用泛型推断类型和笔尖名称
protocol NibIdentifiable {
static var nibNameIdentifier: String { get }
}
// MARK: - Indentifies each storyboard from its classname.
extension NibIdentifiable where Self: UIViewController {
static var nibNameIdentifier: String {
return String(describing: self)
}
}
extension UIViewController :NibIdentifiable
{
}
extension UIViewController {
func instantiate<Controller: UIViewController>(_: Controller.Type) -> Controller where Controller: NibIdentifiable {
guard let controller = Self(nibName:Controller.nibNameIdentifier,bundle:nil) as? Controller else {
fatalError("Could not dequeue cell with identifier: …Run Code Online (Sandbox Code Playgroud) nib ×10
ios ×6
objective-c ×5
xib ×4
xcode ×3
cocoa ×2
iphone ×2
swift ×2
uitableview ×2
generics ×1
nspopover ×1
nswindow ×1
storyboard ×1
xcode4 ×1