1

私はまだこれでかなり新しいので、我慢してください。これを行うように見えるアプリを見たので、ScrollView に Xib をロードできると思いましたが、2 つの異なるクラスについて話しているのです。しかし、とにかく尋ねます-下のビューが移動する間、UIで定義されたボタンが移動しない、静的Xibを上に持つscrollViewを持つ実用的な方法はありますか。cocos2d で簡単に実行できると確信していますが、やりたいことに対しては、少しやり過ぎです。

- - 編集 - -

恥ずかしい思いをするかもしれませんが、考えられる解決策を両方試してみました。ボタンを追加すると、スクロールすると動くボタンが文法的に追加されます。ペン先を追加すると、スクロール画面がスクロールしなくなるようです。ボタンを追加しようとせずに、すべて正常に動作するコードを次に示します。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"View Loaded");


    [mdm setMapSetupInfoWithRows:60 columns:90 cellSize:32];
    [mdm initMapDataWithOriginsUsingCenter:TRUE];

    NSLog(@"MapViewContoller.mapArrayCount = %d",[[mdm mapArray]count]);

    // create the MapView with the screen size create by MapDataManager
     mapView = [[MapView alloc] initWithFrame:[mdm mapRect]];

    // Create the UIScrollView to have the size of the window, matching the window (screen) size
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[mdm windowRect]];
    [scrollView setBounds:[mdm windowRect]];

    // Tell the scrollview how big it is and set other options
    [scrollView setContentSize:[mdm mapRect].size];
    [scrollView setBounces:NO];
    [scrollView setMinimumZoomScale:.5];
    [scrollView setMaximumZoomScale:10];
    [scrollView setDelegate:self];
    [scrollView setBackgroundColor:[UIColor darkGrayColor]];


    //add the MapView as a subview of the scrollView
    [scrollView addSubview:mapView];

    //add the scrollView to the current one....
    [[self view] addSubview:scrollView];

    [[NSBundle mainBundle] loadNibNamed:@"MapViewController" owner:self options:nil];


    [self generNewMap];
}

他に何か間違ったことをしようとしていますか? これをもっと見ると、それは実行可能に見えます。

4

2 に答える 2

0

秘訣は、XIB をロードするときに所有者を指定することです。例えば:

  • UIViewController で IBOutlet を定義する

    @property (非アトミック、強力) IBOutlet UIView* myScrollViewContent

  • XIB を作成し、所有者を UIViewController クラスとして指定します

  • XIB で定義されたコンポーネントを UIViewController クラスで定義されたアウトレットに接続します。

次に、コードで次のようにします。

//Because the owner is 'self' the bundle loader will inject any properties defined . . 
//. . . . and wired in the XIB to the owner's outlets
[[NSBundle mainBundle] loadNibNamed:@"MyXibName" owner:self options:nil];
//Now do something with self.myScrollViewContent - ie add it to the scroll view. 

カスタム スクロール ビュー サブクラス

必要に応じて、独自のスクロール ビュー サブクラスを作成し、そこにアウトレットを指定することで、同じアプローチを使用できるようにする必要があります。. . UIView はサブクラスに直接ロードされます。. . (サブビューとして追加する必要があります)。

より複雑な要件については、個人的には純粋なコードでビューを構築するのが好きですが、XIB を使用してきちんと整理することもできます。

于 2013-04-25T23:55:01.670 に答える