2

私は iOS プログラミングの初心者 (私は Java を知っています) で、単純な UISwitch に問題があります。2 つのビューを持つタブ ベースのアプリケーションがあります。最初のビュー: データ (シングル ビュー)。2 番目のビュー: 設定 (TableView)。

最初のビューでいくつかの UILabels を作成し、設定ビューでいくつかの TableView セルを作成することから始めました。ここで、(テーブル ビュー セルにある) スイッチがオンのときに、最初のビューのラベルに「YES」、それ以外の場合は「NO」と表示されるようにしたいだけです。本当にシンプルなもの。

私の質問は: FirstView.m で UISwitch にアクセスするにはどうすればよいですか? SecondView.h は既にインポートしています。しかし、私の FirstView が SecondView からすべてのものにアクセスするにはどうすればよいでしょうか?

私はグーグルで検索して見つけました:SecondViewController secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];

SecondView の StoryboardID を secondView に設定しました。しかし、うまくいきません。

誰か助けてくれませんか?

編集:

これが私のコードです:

FirstViewController ビューが表示されました メソッド:

    -(void)viewDidAppear:(BOOL)animated
{
    BOOL onOff = secondView.mySwitch.on;
    if (onOff){
        label.text = @"On";
    }
    else{
        label.text = @"Off";
    }
}

これが私のFirstVIewのviewDidLoadです

    - (void)viewDidLoad
{
    [super viewDidLoad];

    secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];
}

私の.hファイルで私はプロパティを作成しました

@property SecondViewController *secondView;

そして、.m ファイルで @synthesize します。

ストーリーボード ID を設定したことは確かです。別の何かを入力してもプログラムが起動しないからです。

SecondViewController.hi に Switch を追加しました

@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;

右クリックでソースコードにドラッグして追加しました。

だから私はクラスにアクセスできないようです

+++++++++++++++++ 編集 2: ++++++++++++++++++++++++

これは私の完全なプロジェクトです。基本的には、UITableView を 2 番目のビューとして持つ単なるタブ付きテンプレートです。FirstView は singleView です。だからここに私のプロジェクトがあります:

FirstViewController.h:

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *lblSwitch;
@property SecondViewController *secondView;

@end

FirstViewController.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize secondView, lblSwitch;


- (void)viewDidLoad
{
    [super viewDidLoad];

    secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void) viewDidAppear:(BOOL)animated{

    if (secondView.mySwitch.isOn){

        NSLog(@"First View: Switch is on!");
        lblSwitch.text = @"Switch is on!";

    }
    else {

    NSLog(@"First View: Switch is off!");
    lblSwitch.text = @"Switch is off!";

    }


}


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

@end

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UITableViewController
@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize mySwitch;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void) viewDidAppear:(BOOL)animated {

    if (mySwitch.isOn) NSLog(@"Switch is on");
    else NSLog(@"Switch is off");

}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

@end

それがすべてのコードです。ここにmain.storyboardのスクリーンショットがあります ここに写真があります

4

3 に答える 3

2

ここに 2 つのクラスを投稿します。

コピーして貼り付けるだけです。

SwitchViewController.h (最初のビュー コントローラー)

@class LabelViewController;
@interface SwitchViewController : UIViewController

@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
- (IBAction)switch:(id)sender; 
//connect the above from storyboard.
@property (strong, nonatomic) LabelViewController *secondView;

@end

SwitchViewController.m

#import "SwitchViewController.h"
#import "LabelViewController.h"

@interface SwitchViewController ()

@end

@implementation SwitchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];
    self.view.backgroundColor = [UIColor lightGrayColor];
}

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

- (IBAction)switch:(id)sender
{
    UISwitch *theSwitch = (UISwitch*) sender;
    [self addChildViewController:self.secondView];
    [self.view addSubview:self.secondView.view];
    self.secondView.myLabel.text = (theSwitch.isOn ? @"On" : @"Off");
}
@end

LabelViewController.h (2 番目のビュー コントローラー)

#import <UIKit/UIKit.h>

@interface LabelViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIButton *backToFirstView;
- (IBAction)backToFirstView:(id)sender; 
//connect the above from storyboard.
@end 

LabelViewController.m

#import "LabelViewController.h"

@interface LabelViewController ()

@end

@implementation LabelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor darkGrayColor];
}

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

- (IBAction)backToFirstView:(id)sender
{
    [self removeFromParentViewController];
    [self.view removeFromSuperview];
    NSLog(@"If it works, buy me a beer.");
}
@end

ストーリーボード: 2 を作成しますUIViewControllers。そこにいる間に、 Storyboard ID をsecondViewLabelViewControllerとして設定します。

ストーリーボードのスクリーンショットは次のとおりです。

絵コンテ

SUPER EDIT:

2 つのクラスとストーリーボードのスナップショットがあります。

あなたの最初のView(Label as のものIBOutlet

YourFirstViewController.h

#import <UIKit/UIKit.h>

@interface YourFirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end

YourFirstViewCntroller.m

#import "YourFirstViewController.h"
#import "YourTableViewController.h"

@interface YourFirstViewController ()

@property(strong, nonatomic) YourTableViewController *tableView;

@end

@implementation YourFirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    self.tableView = [self.tabBarController.viewControllers objectAtIndex:1];
    self.view.backgroundColor = [UIColor darkGrayColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"view will appear in first view");
    [super viewWillAppear:animated];
    self.myLabel.text = (self.tableView.mySwitsch.isOn ? @"ON" : @"OFF");
}

YourTableViewController.h (スイッチ用の がありますIBOutlet)

#import <UIKit/UIKit.h>

@interface YourTableViewController : UITableViewController
@property (weak, nonatomic) IBOutlet UISwitch *mySwitsch;
@end

YourTableViewController.m

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"view will apear table view");
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load in table view");
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell.contentView addSubview:self.mySwitsch];
    }
// Configure the cell...

    return cell;
}

絵コンテのスナップショット

***** スーパースーパー編集 *****

あなたのUINavigation Controller周りを保ちたい場合:

最初のView Controller.mで

`viewDidLoad' で次のように置き換えます。

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Do any additional setup after loading the view, typically from a nib.
    self.navController = [self.tabBarController.viewControllers objectAtIndex:1];
    self.tableView = (YourTableViewController*) [self.navController.viewControllers objectAtIndex:0];
    self.view.backgroundColor = [UIColor darkGrayColor];
}

プロパティを追加する@property(strong, nonatomic) UINavigationController *navController;

Tested.
于 2013-10-10T21:39:38.483 に答える
0

まず、UISwitch から settingsviewcontroller への IBOutlet を .h ファイルに作成し、switcher という名前を付ける必要があります。次に、このコードを使用して、スイッチがオンになっているかどうかを確認します。

 SecondViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];
 BOOL onOrOff = switcher.on;
 if (onOrOff == YES) NSLog(@"Switch is on");
 if (onOrOff == NO) NSLog(@"Switch is off");
于 2013-10-08T14:07:54.837 に答える