簡體   English   中英

我是否需要為每個UIVIew創建變量並鏈接IBOutlet?

[英]Do I need to create variables and link IBOutlet for every UIVIew?

我有一個View Controller,它可以將UIView對象交換進出。 可能有數百種不同的視圖,每種視圖都有自己的行為。

在當前的MainWindow.xib文件中,當前具有:

File's Owner     UIApplication
First Responder  UIResponder
AppDelegate      AppDelegate
  -Cover         Cover
Window           UIWindow
Table of Contents  TableOfContents
page1            Page1
page2            Page2
page...n         Page...n

AppDelegate聲明窗口和viewController。 這很基本。

MainViewController.h

#import <UIKit/UIKit.h>


@class TableOfContents, Page1;

@interface MainViewController : UIViewController {

    TableOfContents *tableOfContents;
    Page1 *page1;
    Page2 *page2;
    Page...n *page...n;
}

@property (nonatomic, retain) IBOutlet TableOfContents *tableOfContents;
@property (nonatomic, retain) IBOutlet Page1             *page1;
@property (nonatomic, retain) IBOutlet Page2             *page2;
@property (nonatomic, retain) IBOutlet Page...n          *page...n;

-(IBAction)funcGoToPage:(id)sender;

@end

MainViewController.m

#import "MainViewController.h"
#import "TableOfContents.h"
#import "Cover.h"
#import "Page1.h"
#import "Page2.h"
#import "Page...n.h"

@implementation MainViewController

@synthesize page1, page2, page...n tableOfContents;
@synthesize pageID, pagesPathFile, pagesPath;


-(IBAction)funcGoToPage:(id)sender{

    //[[self view] removeFromSuperview];
    [self.view addSubview:self.tableOfContents];
}

目前,相應的UIView類還很裸露,因此我將避免發布它們。

現在funcGoToPage剛剛調出tableOfContents。 最終,我會根據點擊的內容將其放到不同的位置。

當前,每個頁面都設置為IBOutlet,並從MainViewController鏈接到Interface Builder中的相應UIView。 這樣,必須將每個頁面設置為變量,並鏈接到IB中,以創建變量,出口和連接的集線器。

我的問題是:有沒有一種動態創建這些連接的方法,以便我可以使用funcGoToPage函數交換它們,而無需將它們設置為IBOutlet?

當筆尖被加載時,其所有內容都被加載。 如果您在一個筆尖中有很多視圖,則會很快耗盡內存。

我將每個頁面放在單獨的筆尖中,然后在需要時加載筆尖:[[NSBundle mainBundle] loadNibNamed:@“ nibNameWithoutExtension” owner:self options:nil];

為了這個工作:

  • IBOutlet (例如newPage添加到self引用的任何內容
  • nibNameWithoutExtension的File Owner設置為任何self引用
  • nibNameWithoutExtension的視圖加入到文件所有者的newPage出口中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM