0

这就是我想要做的:使用 2 个您可以选择的选择器视图。然后人员单击按钮以根据用户选择的内容进行处理。这两个参数从选择器中使用,然后数据显示在底部。

我知道 C++ 并使用菜单样式创建了一个 ddos​​ 版本,但我对 Objective-C 和 iphone 开发非常陌生。我只是需要帮助将这些物体绑在一起。第一个选择器将用作参数,第二个将用作带有变量的开关,并在按钮被按下到底部的显示屏上时计算一些东西。我知道这是一个巨大的要求,但如果有人能指出我正确的方向,我会很高兴。

-(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

但这似乎是一个直截了当的问题,我可以就一种解决方法提供一些一般性的想法。

我将使用纯编码而不是 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 回答