我是iOS编程的新手(我知道Java),我在使用简单的UISwitch时遇到了麻烦。我有一个带有两个视图的基于选项卡的应用程序。第一视图:数据(单视图)。第二视图:设置(TableView)。

我首先在“第一个”视图上创建一些UILabel,然后在“设置”视图中创建一些TableView单元。现在,我只希望打开一个开关(位于表视图单元格中)时,第一个视图上的标签应显示“是”,否则为“否”。真的很简单。

我的问题是:如何在FirstView.m中访问UISwitch?我已经导入了SecondView.h。但是,我的FirstView如何从SecondView中访问所有内容?

我在Google上搜索后发现: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它。

我确定我设置了Storyboard ID,因为当我键入其他内容时,该程序甚至无法启动。

在SecondViewController.h中,我添加了Switch
@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;

我通过右键单击将其拖动到源代码中来添加它。

所以看来我没有上课的机会

+++++++++++++++++ EDIT2:++++++++++++++++++++++++

这是我完整的项目。基本上,它只是选项卡式模板,带有UITableView作为第二视图。 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的屏幕截图
Here is the Photo

最佳答案

我将在这里为您发布2个 class :

只需复制并粘贴即可。

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(您的第二视图控制器)
#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
当您在那里时,将LabelViewController的情节提要ID设置为 secondView

这是情节提要的屏幕截图:
SUPER EDIT:
有两个 class 和一个故事板快照。

您的firstView(标签为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;
}

***** super super 编辑*****

如果您想保留自己的UINavigation 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.

09-25 19:00