0

これが私がやりたいことです: 選択できる 2 つのピッカー ビューを使用します。次に、ボタンをクリックして、ユーザーが選択したものからプロセスを実行します。2 つの引数がピッカーから使用され、データが下部に表示されます。

私は C++ を知っており、メニュー スタイルを使用してこれの ddos​​ バージョンを作成しましたが、Objective-C と iPhone の開発は初めてです。オブジェクトを結び付けるのに助けが必要です。最初のピッカーは引数として使用され、2 番目のピッカーは変数のスイッチとして使用され、下部のディスプレイにボタンが押されたときに何かを計算します。これが大きな要求であることは承知していますが、誰かが私を正しい方向に向けることができれば幸いです.

-(void)buttonTapped:(id)sender
{
    int maxRow = [myPicker selectedRowInComponent:0];
    int programRow = [myPicker selectedRowInComponent:1];

    NSLog(@"One Rep Max Chosen: %@", [oneRepMax objectAtIndex:maxRow];
    NSLog(@"Program Chosen: %@", [program objectAtIndex:programRow];

    NSString *allInformation = [NSString alloc] initWithFormat:@%@\n%@", [oneRepMax objectAtIndex:maxRow],[program objectAtIndex:programRow]];

    int level = programRow;
    int max = [[oneRepMax objectAtIndex:maxRow]intValue];
    int set1;
    int set2;
    int set3;
    int set4;

    switch(level)
    {
        case 0:
        {
             set1 = max * 0.7;
             set2 = max * 0.71;
             set3 = max * 0.72;
             set4 = max * 0.73;
             break;
        }
        case1:
    }
4

1 に答える 1

0

まあ、簡単な答えは、あなたがObjective Cについてどれだけ知っているか分からないので、本を手に入れることです:D

しかし、これは率直な質問のように思えます。私は、それを行うための 1 つの方法について、いくつかの一般的なアイデアを提供できます。

Interface Builder ではなく純粋なコーディングを使用しますが、Interface Builder を使用することをお勧めします。これは、インターフェイスのセットアップを実際のコード ロジックから分離するためです。

このコードは、自動参照カウント (ARC)が使用されていることを前提としています

ViewController.h ファイル

#import <UIKit/UIKit.h>

// --------------------------------------------------------------------------------
// The UIPickerViewDelegate is a protocol (kinda of like an abstract class in C++)
// you need to implement the methods (or functions in C++ terms) of the protocol
//
// The UIPickerViewDataSource also a protocol to tell your ViewController to expect
// a method that provides your UIPickerViews the data it needs to display.
// --------------------------------------------------------------------------------
@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
    // UIPickerView can have multiple components (multiple roller thing)
    UIPickerView *myPicker;

    // arrays to hold your data
    NSArray *list1;
    NSArray *list2;

    // our button to do something when pressed
    UIButton *btnCalculate;
}

@end

ViewController.m ファイル

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    myPicker.showsSelectionIndicator = YES;

    // ------------------------------------------------------------------
    // these two lines are compulsory, it is saying the picker view's
    // delegate and datasource is this view controller itself
    // ------------------------------------------------------------------
    myPicker.delegate = self;
    myPicker.dataSource = self;

    // ------------------------------------------------------------------
    // Here we're specifying what we want to show in each row of the
    // picker view. Each of these array will be loaded into each of
    // the picker view components (see below)
    // ------------------------------------------------------------------

    // -------------------------------------
    // |    apple        |     option 1    |
    // |    banana       |     option 2    |
    // |    orange       |     option 3    |
    // |    mango        |                 |
    // |    peach        |                 |
    // -------------------------------------

    list1 = [[NSArray alloc] initWithObjects:@"apple", @"banana", @"orange", @"mango", @"peach", nil];
    list2 = [[NSArray alloc] initWithObjects:@"option1", @"option2", @"option3", nil];


    btnCalculate = [[UIButton alloc] initWithFrame:CGRectMake(120, 300, 100, 50)];
    btnCalculate.backgroundColor = [UIColor blackColor];
    [btnCalculate setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnCalculate setTitle:@"Calculate" forState:UIControlStateNormal];

    // this is how we add a callback method for the button tap event
    [btnCalculate addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:myPicker];
    [self.view addSubview:btnCalculate];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// ------------------------------------------------------------
// Your UIPickerViewDataSource methods you need to implement
// ------------------------------------------------------------
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    // we're telling the our picker we want 2 rollers in a single picker view
    // like this:

    // -------------------------------------
    // |                 |                 |
    // |                 |                 |
    // |   component 1   |   component 2   |
    // |                 |                 |
    // |                 |                 |
    // -------------------------------------

    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    // -------------------------------------------------------------------------
    // this is telling the picker how many rows to expect for each picker
    // it must match up with our data source (which in this case is our
    // list1 and list2).
    // -------------------------------------------------------------------------

    // -------------------------------------
    // |                 |                 |
    // |                 |                 |
    // |     5 fruits    |     3 options   |
    // |                 |                 |
    // |                 |                 |
    // -------------------------------------

    if(component == 0)
    {
        return list1.count;
    }
    else
    {
        return list2.count;
    }
}

// ------------------------------------------------------------
// Your UIPickerViewDelegate methods you need to implement
// ------------------------------------------------------------

// ------------------------------------------------------------
// This method will return the actual text into the picker view
// components.
//
// Note: This is for basic UIPickerView, if you want more fancy
// looking rows, you need to implement the other delegate method
// instead of this one.
// ------------------------------------------------------------
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0)
    {
        return [list1 objectAtIndex:row];
    }
    else
    {
        return [list2 objectAtIndex:row];
    }
}

// ------------------------------------------------------------
// Your callback method for when user taps on your button
// ------------------------------------------------------------
-(void)buttonTapped:(id)sender
{
    int fruitRow = [myPicker selectedRowInComponent:0];
    int optionRow = [myPicker selectedRowInComponent:1];

    // This will print the result into Xcode's console window
    NSLog(@"Fruit chosen: %@", [list1 objectAtIndex:fruitRow]);
    NSLog(@"Option chosen: %@", [list2 objectAtIndex:optionRow]);

    // This will show a popup alert view with same information
    NSString *allInformation = [[NSString alloc] initWithFormat:@"%@\n%@",
                                [list1 objectAtIndex:fruitRow], [list2 objectAtIndex:optionRow]];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information"
                                                    message:allInformation
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
    [alert show];
}


@end

最終結果

次のような結果になるはずです。

ここに画像の説明を入力

そこから、メソッド-(void)buttonTapped:(id)senderを変更して、好きなことを行います。

それが役立つことを願っています。

于 2013-05-27T05:10:40.950 に答える