- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我知道这很直截了当,但经过太多的牵扯,我离解决方案还差得很远。
我看过解释如何使用 XIB 和所有内容创建 View 的教程。但他们都没有解决我在这里遇到的情况。
我有一个 XIB 文件,它是一个自定义的 UIView 子类,几乎没有标签和按钮。 UIView 子类是可重用的,这就是我不能在任何单个 View Controller 中设置 socket 的原因。因此,我将此 View 的各个控件( subview )存储在我的自定义 UIView 本身中。这是合乎逻辑的,因为任何 View Controller 都不应拥有此自定义 View 的 subview ,该自定义 View 将包含在每个 View Controller 中。
问题是,我不知道如何完全初始化整个 UI。
这是我的 UIView 子类代码:
@interface MCPTGenericView : UIView
+(id)createInstance : (bool) bPortrait;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *titleView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *logoButton;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
@property (weak, nonatomic) IBOutlet UIButton *menuButton;
@end
稍后,我还计划将同一个 XIB 文件用于此 UIView 的横向,并且我计划在同一个 XIB 中使用具有横向控件的上述 socket 。
下面是实现:
@implementation MCPTGenericView
//@synthesize topView, titleLabel, titleView;
+(id)createInstance : (bool) bPortrait
{
UIView * topLevelView = nil;
MCPTGenericView * instance = [MCPTGenericView new];
NSArray * views = [[NSBundle mainBundle] loadNibNamed:@"MoceptGenericView" owner:instance options:nil];
int baseTag = (bPortrait)?PORTRAIT_VIEW_TAG_OFFSET:LANDSCAPE_VIEW_TAG_OFFSET;
// make sure customView is not nil or the wrong class!
for (UIView * view in views)
{
if (view.tag == baseTag)
{
topLevelView = view;
break;
}
}
instance.topView = (MCPTGenericView *)[topLevelView viewWithTag:baseTag + 1];
instance.searchTextField = (UITextField *)[topLevelView viewWithTag:baseTag + 2];
instance.menuButton = (UIButton *)[topLevelView viewWithTag:baseTag + 3];
instance.logoButton = (UIButton *)[topLevelView viewWithTag:baseTag + 4];
instance.titleView = [topLevelView viewWithTag:baseTag + 5];
instance.titleLabel = (UILabel *)[topLevelView viewWithTag:baseTag + 6];
return instance;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MCPTGenericView" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self addSubview: self.titleView];
[self addSubview:self.topView];
}
- (id) init
{
self = [super init];
if (self)
{
[[NSBundle mainBundle] loadNibNamed:@"MCPTGenericView" owner:self options:nil];
[self addSubview:self.topView];
[self addSubview:self.titleView];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
[[NSBundle mainBundle] loadNibNamed:@"MCPTGenericView" owner:self options:nil];
[self addSubview:self.topView];
[self addSubview:self.titleView];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
有用的东西:
我成功地从我的 View Controller 调用了 initWithFrame:frame
。这样,我可以看到所有控件都已正确初始化。但是,如果我已经绘制了 XIB,为什么还要提供框架呢? loadNibNamed 不应该处理框架设置和布局内容,因为这是 XIB 的预期用途吗?
我也对 loadNibNamed 需要所有者对象的方式感到困惑。为什么我们已经需要一个对象来从 XIB 获取相同的对象?那也是半生不熟的?
请帮忙...
最佳答案
令我困惑的是 loadnibnamed
丢失 xib 布局和导出信息的方式。我终于找到了实现它的方法。
这里是对有效方法的回顾:
1) 假设 MyCustomView 是您的自定义 View 类 - 您将其及其 subview 设计为 XIB 的一部分。您可以通过界面生成器执行此操作,不言自明。
2) 通过 Xcode -> File -> New -> Objective C Class 添加 MyCustomView.h 和 MyCustomView.m(样板)。
3) 接下来,在 MyCustomView.xib 中,设置 File's Owner = MyCustomView(刚刚添加的类名)。 不要触及最顶层的 View 自定义类 - 将其保留为 UIView。否则它会以递归结束!!!
4) 在 MyCustomView.h 中,创建几个与 MyCustomView.xib 中的 subview 对应的导出。
如:
@property (weak) IBOutlet UILabel * label1;
@property (weak) IBOutlet UIButton * button1;
5) 转到 MyCustomView.xib。选择每个 subview (标签、按钮),单击鼠标右键,从“New Referencing Outlet”拖动并将其向上拖动到 File's Owner。
这将弹出一个与您拖动的 subview 类型相匹配的 socket 列表。如果你从一个label拖过来,就会弹出label1,以此类推。这表明您到这一步所做的一切都是正确的。
另一方面,如果您在任何步骤中搞砸了,则不会出现弹出窗口。检查步骤,尤其是 3 和 4。
如果您没有正确执行此步骤,Xcode 将欢迎您出现以下异常:
setValue:forUndefinedKey: this class is not key value coding-compliant for the key
6) 在您的 MyCustomView.m 中,粘贴/覆盖以下代码:
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
NSString * nibName = @"MyCustomView";
[[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] firstObject];
[self addSubview:self.labelContinentName];
}
return self;
}
此步骤至关重要 - 它将您的 socket 值(label1、button1)从 nil 设置为有形 subview ,最重要的是,根据您在 MyCustomView.xib 中设置的内容设置它们的框架。
7) 在您的 Storyboard文件中,添加 MyCustomView 类型的 View - 就像任何其他 View 一样:
它应该可以正常运行!
关于ios - loadNibNamed 如何工作? UIView socket 未使用 loadNibNamed 进行初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20323393/
基于 socket.io 的官方网站 http://socket.io/#how-to-use , 我找不到任何术语。socket.emit 、 socket.on 和 socket.send 之间有
我正在使用 lua-socket 3.0rc1.3(Ubuntu Trusty 附带)和 lua 5.1。我正在尝试监听 unix 域套接字,我能找到的唯一示例代码是 this -- send std
这两者有什么区别? 我注意到如果我在一个工作程序中从 socket.emit 更改为 socket.send ,服务器无法接收到消息,虽然我不明白为什么。 我还注意到,在我的程序中,如果我从 sock
使用套接字在两台服务器之间发送数据是个好主意,还是应该使用 MQ 之类的东西来移动数据。 我的问题:套接字是否可靠,如果我只需要一次/有保证的数据传输? 还有其他解决方案吗? 谢谢。 最佳答案 套接字
引自 this socket tutorial : Sockets come in two primary flavors. An active socket is connected to a
我已经安装了在端口81上运行的流服务器“Lighttpd”(light-tpd)。 我有一个C程序,它使用套接字api创建的服务器套接字在端口80上监听http请求。 我希望从客户端收到端口80上的请
这是我正在尝试做的事情: 当有新消息可用时,服务器会将消息发送给已连接的客户端。另一方面,客户端在连接时尝试使用send()向服务器发送消息,然后使用recv()接收消息,此后,客户端调用close(
如何将消息发送到动态 session 室,以及当服务器收到该消息时,如何将该消息发送到其他成员所在的同一个 session 室? table_id是房间,它将动态设置。 客户: var table_i
这是我尝试但不起作用的方法。我可以将传入的消息从WebSocket连接转发到NetSocket,但是只有NetSocket收到的第一个消息才到达WebSocket后面的客户端。 const WebSo
我正在实现使用boost将xml发送到客户端的服务器。我面临的问题是缓冲区不会立即发送并累积到一个点,然后发送整个内容。这在我的客户端造成了一个问题,当它解析xml时,它可能具有不完整的xml标记(不
尝试使用Nginx代理Gunicorn套接字。 /etc/systemd/system/gunicorn.service文件 [Unit] Description=gunicorn daemon Af
我正在使用Lua套接字和TCP制作像聊天客户端和服务器这样的IRC。我要弄清楚的主要事情是如何使客户端和服务器监听消息并同时发送它们。由于在服务器上执行socket:accept()时,它将暂停程序,
我看了一下ZMQ PUSH/PULL套接字,尽管我非常喜欢简单性(特别是与我现在正在通过UDP套接字在系统中实现的自定义碎片/ack相比),但我还是希望有自定义负载平衡功能,而不是幼稚的回合-robi
我正在编写一个应用程序,其中有多个 socket.io 自定义事件,并且所有工作正常,除了这个: socket.on("incomingImg", function(data) {
在我的应用程序中,我向服务器发送了两条小消息(类似 memcached 的服务)。在类似 Python 的伪代码中,这看起来像: sock.send("add some-key 0") ignored
很抱歉再次发布此问题,但大多数相关帖子都没有回答我的问题。我在使用 socket.io 的多个连接时遇到问题我没有使用“socket.socket.connect”方法,但我从第一次连接中得到了反馈。
我尝试使用 socket.io 客户端连接到非 socket.io websocket 服务器。但我做不到。我正在尝试像这样连接到套接字服务器: var socket = io.connect('ws
我遇到了一个奇怪的问题。在我非常基本的服务器中,我有: server.listen(8001); io.listen(server); var sockets = io.sockets; 不幸的是,套
我正在使用带套接字 io 的sailsjs。帆的版本是 0.10.5。我有以下套接字客户端进行测试: var socketIOClient = require('socket.io-client');
这个问题在这里已经有了答案: What is the fundamental difference between WebSockets and pure TCP? (4 个答案) 关闭 4 年前。
我是一名优秀的程序员,十分优秀!