Iphone 如何使用iOS5在ViewController中为具有多个UIImageView的子视图创建父对象

Iphone 如何使用iOS5在ViewController中为具有多个UIImageView的子视图创建父对象,iphone,model-view-controller,uiimageview,subview,iboutlet,Iphone,Model View Controller,Uiimageview,Subview,Iboutlet,在iOS5的iPhone应用程序中,我试图创建一个控制器对象,其中包含一个包含多个UIImageView的图像子视图。我有一个主视图控制器及其关联的主视图,在此主视图中我有一个多图像子视图,此子视图有十几个UIImageView,如Interface Builder中配置的。我已经创建了UIView的子代,并将其连接为ViewController中ImageSubView的IBOutlet连接。我想在子视图类中为十几个UIImageView创建IBOutlets,但Interface Build

在iOS5的iPhone应用程序中,我试图创建一个控制器对象,其中包含一个包含多个UIImageView的图像子视图。我有一个主视图控制器及其关联的主视图,在此主视图中我有一个多图像子视图,此子视图有十几个UIImageView,如Interface Builder中配置的。我已经创建了UIView的子代,并将其连接为ViewController中ImageSubView的IBOutlet连接。我想在子视图类中为十几个UIImageView创建IBOutlets,但Interface Builder只允许我在主ViewController中创建IBOutlets,而不允许在ImageSubView类中创建IBOutlets。也就是说,当我从其中一个UIImageView中控制拖动时,如果我在MainViewController@interface代码中拖动,而不是在ImageSubView@interface代码中拖动,则只显示IBOutlet的创建。我想从主ViewController中隐藏控制这十几幅图像的细节。我希望主ViewController仅与ImageSubView通信,并让ImageSubView知道如何操作其中的十几个图像。是否需要创建子viewController?如果是,如何在当前主视图中创建子viewController?Interface Builder不允许我将另一个ViewController拖动到当前主视图中。一个屏幕可以有多个ViewController(主控制器和子控制器)

视图层次结构如下所示:

MainViewController
    MainView
       ImageViewFullBackground
       ImageSubView
          UIImageView1
          UIImageView2
          . . . 
          UIImageView12
       Button1
       Button2
       . . .
       Button10
NSArray *subViewsArray = self.view.subviews; //Change this to the name of your view
UIView *view1 = [subViewsArray objectAtIndex:0]; //This selects view at position 0
NSLog(@"View1: %@",view1); //You can see what view you extracted (the properties)
作为对Allen的回应,这里是我的ImageSubView标题。该类实际上称为UILedNumericView

//
//  UINumericLabel.h
//  UILedNumericView
//
//  Created by Jon D. Newbill on 2/18/12.
//  Copyright (c) 2012 Bitworks Systems Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface UILedNumericView : UIView

@property (copy,nonatomic) NSString * text;

// This is the numeric value of the LED display as a double
@property (nonatomic) double value;

// Returns true if the numeric display contains a decimal point
@property (readonly,nonatomic) BOOL hasDecimalPoint;

// Returns true if numeric display contains an exponent
@property (readonly,nonatomic) BOOL hasExponent;

// Returns true if numeric display is equal to zero.
@property (readonly,nonatomic) BOOL isZero;

// Returns number of characters in entire display
@property (readonly,nonatomic) NSUInteger length;

// Returns zero based character index of the E in the display
@property (readonly,nonatomic) NSUInteger exponentIndex;

// Returns or sets mantissa portion of display.  This is everything
// preceding the "E" on the display or the entire display if it does
// not contain an "E";
@property (copy, nonatomic) NSString * mantissa;

// Returns or sets exponent portion of display.  This is everything
// following the "E" on the display.  If no E exists then an empty
// string is returned.  When set to a non-zero length string an E will
// be added to the display.
@property (copy, nonatomic) NSString * exponent;

// Returns maximum allowed characters in display
// This includes digits, sign, decimal point and exponent
+ (NSUInteger) maxLength;

@end
//
//UINumericLabel.h
//UILedNumericView
//
//由Jon D.Newbill于2012年2月18日创作。
//版权所有(c)2012 Bitworks Systems Inc.保留所有权利。
//
#进口
@界面UILedNumericView:UIView
@属性(复制,非原子)NSString*文本;
//这是LED显示为双精度的数值
@属性(非原子)双值;
//如果数字显示包含小数点,则返回true
@属性(只读,非原子)BOOL hasDecimalPoint;
//如果数字显示包含指数,则返回true
@属性(只读,非原子)布尔指数;
//如果数字显示等于零,则返回true。
@属性(只读,非原子)BOOL为零;
//返回整个显示中的字符数
@属性(只读,非原子)整数长度;
//返回显示中E的从零开始的字符索引
@属性(只读、非原子)整数指数索引;
//返回或设置显示部分的尾数。这就是一切
//在显示屏或整个显示屏上的“E”前面(如果是
//不包含“E”;
@属性(复制,非原子)NSString*尾数;
//返回或设置显示的指数部分。这就是一切
//在显示屏上的“E”之后。如果不存在E,则为空
//返回字符串。当设置为非零长度字符串时,E将
//将被添加到显示中。
@属性(复制,非原子)NSString*指数;
//返回显示中允许的最大字符数
//这包括数字、符号、小数点和指数
+(整数)最大长度;
@结束

您可以从interfacebuilder中已获得IBOutlet的任何视图中获取子视图,如下所示:

MainViewController
    MainView
       ImageViewFullBackground
       ImageSubView
          UIImageView1
          UIImageView2
          . . . 
          UIImageView12
       Button1
       Button2
       . . .
       Button10
NSArray *subViewsArray = self.view.subviews; //Change this to the name of your view
UIView *view1 = [subViewsArray objectAtIndex:0]; //This selects view at position 0
NSLog(@"View1: %@",view1); //You can see what view you extracted (the properties)
您不需要创建viewcontroller或其他东西。然而,如果你想,这是可能的,但我不相信这是你想要的

编辑:IBOutlets,顾名思义,用于告诉编译器在接口生成器中创建指向项的指针,因此,如果您遵循我发布的内容,您仍在以编程方式创建这些指针。如果您想像IBOutlets一样拥有这些功能的“全局”便利性,只需为UIView创建一个属性,而不是像我那样在本地声明它们

Edit2:忘记提及视图的顺序与interface builder中从上到下的顺序相同。(除非您以编程方式更改它们)

inti,x,y;
x=y=2;
NSArray*viewsToRemove=[mainScroll子视图];
对于(UIView*v在viewsToRemove中)[v从SuperView移除];
useD=[NSUserDefaults standardUserDefaults];
imageArray=[useD objectForKey:LIVE];

对于(i=1;i首先:在ImageSubView标题(我想是UILedNumericView.h)中声明IBOutlets。每个UIImageView一个 第二:转到Interface Builder,确保将ImageSubView的类设置为UILedNumericView,因为这是ImageSubView的类。 第三:控制从每个imageView#拖动到Interface Builder左侧的ImageSubView(带有视图树的图解视图)。 第四:出现IBOutlet列表,您可以选择正确的一个。 第五:合成所有iboutlet,并在UILedNumericView.m中编写一些方法来管理它们

如果我理解了你的问题,那就是显而易见的解决办法


通过第三步可以选择ImageSubView并从右窗格中完成,在右窗格中有所选对象出口的完整列表,您可以将其拖动到ImageView。

为什么不简单地将ImageSubView设计为一个单独的UIView。在它自己的.xib文件中布局该子视图。然后确保您的子类实现
initWithCoder:
。然后可以将子视图添加到ImageViewController。只需确保.xib中的所有对象都定义了自定义类即可


请告诉我一些图片或示例项目是否有助于您更好地理解这一概念。有时IB在幕后所做的工作可能看起来很神奇,但一旦您理解了它,您就会看到它背后的强大威力。

首先,如果您想将逻辑隐藏到与
MainViewController我建议您创建第二个控制器,例如
ImageSubViewController
。您可以通过专用NIB文件定义它的外观,在该文件中,您将所有
UIImageViews
链接到父级
ImageSubViewController
及其