2

im a newbie in iOS Programming (i know java) and i have trouble with a simple UISwitch. I have a Tab-based Application with two Views. First View: Data (Single View). Second View:Settings (TableView).

I started with creating some UILabels on my First view and with some TableView Cells in the Settings-view. Now i just want that when a Switch (which is in a table view cell) is on, a label on the first View should say "YES", else "no". Just something really simple.

My question is: How can i get access on my UISwitch in the FirstView.m? I imported the SecondView.h already. But how can my FirstView get access to all the stuff from the SecondView?

I searched on google and found : SecondViewController secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];

I set the StoryboardID of my SecondView to secondView. But it doesn't work.

Can someone help me please?

EDIT:

Here is a my Code:

FirstViewController View did appear Method:

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

Here is the viewDidLoad of my FirstVIew

    - (void)viewDidLoad
{
    [super viewDidLoad];

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

in my .h file i created a property

@property SecondViewController *secondView;

And in the .m file i @synthesize it.

I'm sure that i set the Storyboard ID, because when i type in something different, the program doesnt even start.

in the SecondViewController.h i added the Switch

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

I added it by drag it into the source code with a right click.

So it seems that i dont get access to the class

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

This is my complete Project. Basically it is just the Tabbed Template, with a UITableView as the Second View. FirstView is a singleView. So here is my project:

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

That is all the code. Here is a screenshot of the main.storyboard Here is the Photo

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

切换视图控制器.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 

标签视图控制器.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 设置LabelViewControllersecondView

这是故事板的屏幕截图:

故事板

SUPER EDIT:

有两个类和一个故事板的快照。

你的 firstView (带有标签的那个IBOutlet

YourFirstViewController.h

#import <UIKit/UIKit.h>

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

YourFirstViewController.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留在身边:

在你的第一个视图 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

首先,您必须在 .h 文件中创建一个从 UISwitch 到 settingsviewcontroller 的 IBOutlet,并将其命名为 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 回答
0

基本上你需要在 SecondView 中创建一个 UISwitch 变量的@property。然后通过 SecondView 变量访问它

于 2013-10-08T14:12:23.257 回答