在我的程序中,我必须像在主UIViewController中的自定义控件一样声明和使用多个子UIViewController。但是,当我单击一个子UIView Controllers的按钮时,出现了EXC_BAD_ACCESS错误。请参见以下我的示例代码结构。

范例1:

//--Child-------------------------------------------------------------------
@interface subUIViewController<UIViewController>

     -(IBAction)myButtonClick:(id)Sender;

-@implementation subUiViewController

     -(IBAction)myButtonClick:(id)Sender
     {
          NSLog(@"Button Click Event.....");
     }

//---Main--------------------------------------------------------------------

@interface mainUIViewController<UIViewController>

@implementation mainUIViewController

     -(void)viewDidLoad
     {
          [super viewDidLoad];
          subUiViewController *subViewController=[[subUiViewController alloc] initWithNibNameAndArtist:@"subUiViewController" bundle:nil];
          [self.view addSubView: subViewController.view];
     }

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

但是,如果我在@property (retain,nonatomic) subUiViewController *subViewController中用MainViewController声明,那么它将正常工作。请参见以下代码。

范例2:
//---Child------------------------------------------------------------------
@interface subUIViewController<UIViewController>

     -(IBAction)myButtonClick:(id)Sender;

@implementation subUiViewController

     -(IBAction)myButtonClick:(id)Sender
     {
          NSLog(@"Button Click Event.....");
     }

//----Main-----------------------------------------------------------------


@interface mainUIViewController<UIViewController>
     @property(retain,nonatomic) subUiViewController *subViewController;

@implementation mainUIViewController
     @synthesize subViewController;

     -(void)viewDidLoad
     {
          [super viewDidLoad];
          subViewController=[[subUiViewController alloc] initWithNibNameAndArtist:@"subUiViewController" bundle:nil];
          [self.view addSubView: subViewController.view];
     }


//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

我不知道为什么示例1不能正常工作,而示例2却可以正常工作。对我来说,我需要使用示例1,因为我需要以编程方式基于Core Data Return创建子ui视图控制器。您能帮我指出出什么问题吗?如何在代码中动态声明子ui视图控制器?

最佳答案

终于,在看到EricLars0n和user523234的建议之后,我可以解决我的问题。我很感激。这是方法。

@interface mainUIViewController<UIViewController>
     @property(retain,nonatomic) NSMutableArray *myViewControllers;

@implementation mainUIViewController
     @synthesize myViewControllers;

     -(void)viewDidLoad
     {
          [super viewDidLoad];

          //Sub View Controller 1
          subUiViewController *subViewController1=[[subUiViewController alloc] initWithNibName:@"subUiViewController" bundle:nil];
          [self.myViewControllers addObject:subViewController1];
          [self.view addSubView: subViewController1.view];

          //Sub View Controller 2
          subUiViewController *subViewController2=[[subUiViewController alloc] initWithNibName:@"subUiViewController" bundle:nil];
          [self.myViewControllers addObject:subViewController2];
          [self.view addSubView: subViewController2.view];
     }

我使用一个属性类型NSMutableArray ,并将以编程方式创建的子UIViewControllers存储到此NSMutableArray中。所以,我的孩子UiViewControllers没有办法在viewDidUnload之前停止。

谢谢你们!

关于ios - 在主UiViewController中动态声明子UiViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8528855/

10-14 19:59