0

我有一个简单的实用程序应用程序,带有 MainViewController.m & h 和 FlipsideViewController.m & h。在我的故事板中,我在 MainViewController 上有一个按钮。我希望能够单击按钮并在 FlipsideViewController.m 中运行一个方法,这可能吗?这是我的第一个应用程序,我完全是新手。欢迎所有意见/建议。

enter code here

我在我的 FlipsideViewController.m 中有这个,这是我点击按钮时想要调用的。

- (void)SaveFPQData
{
NSLog(@"Data Saved");
}

这就是我在 MainViewController.m 中所拥有的

- (IBAction)saveButton:(id)sender
{

}

这是我到目前为止的代码;

主视图控制器.h

#import "FlipsideViewController.h"
#import "sqlite3.h"
#import "FPQCheck.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *checkField;
@property (weak, nonatomic) IBOutlet UITextField *commentsField;
@property (weak, nonatomic) FlipsideViewController *flipsidecontroller;

- (IBAction)saveButton:(id)sender;
- (IBAction)showHistoryButton:(id)sender;


@end

主视图控制器.m

#import "MainViewController.h"
#import "FlipsideViewController.h"
#import "sqlite3.h"


@interface MainViewController ()

@end


@implementation MainViewController

- (void)viewDidLoad
{


[super viewDidLoad];
}

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


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

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showAlternate"]) {
    [[segue destinationViewController] setDelegate:self];
}
}


- (IBAction)saveButton:(id)sender
{
[self.flipsidecontroller SaveFPQData];


//[[NSNotificationCenter defaultCenter] postNotificationName:@"SaveFPQData" object:nil];
}

- (IBAction)showHistoryButton:(id)sender

{

}
@end

FlipSideViewController.h

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

@class FlipsideViewController;

@protocol FlipsideViewControllerDelegate

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;


@end

@interface FlipsideViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>


@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;

-(void)SaveFPQData;


- (IBAction)done:(id)sender;
- (IBAction)deleteEntry:(id)sender;



@end

FlipSideViewController.m

#import "FlipsideViewController.h"
#import "MainViewController.h"


@interface FlipsideViewController ()
{
NSMutableArray *arrayOfCheck;
sqlite3 *fpqDB;
NSString *dbPathString;

}

@end

@implementation FlipsideViewController

- (void)viewDidLoad
{

/*
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(SaveFPQData)
                                             name:@"SaveFPQData"
                                           object:nil];
 */


[super viewDidLoad];
arrayOfCheck = [[NSMutableArray alloc]init];
[self creatOrOpenDB];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];

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

}

- (void)SaveFPQData
{
NSLog(@"Data Saved");

}


-(void)creatOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:@"FPQ.db"];

char *error;

NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
    const char *dbPath = [dbPathString UTF8String];

    //create db

    if (sqlite3_open(dbPath, &fpqDB)==SQLITE_OK) {
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS FPQ (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, CHECK INTEGER, COMMENTS TEXT)";
        sqlite3_exec(fpqDB, sql_stmt, NULL, NULL, &error);
        sqlite3_close(fpqDB);            
    }
}
}


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

#pragma mark - Actions

- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];}

- (IBAction)deleteEntry:(id)sender {

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayOfCheck count];
}

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

if (!cell){
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

FPQCheck *fpqCheck = [arrayOfCheck objectAtIndex:indexPath.row];

NSString *nameANDcheck = [NSString stringWithFormat:@"%@%d", fpqCheck.name, fpqCheck.check];

cell.textLabel.text = nameANDcheck;
cell.detailTextLabel.text = fpqCheck.comments;

return cell;

}

@end
4

1 回答 1

1

你主要有两种方式:

  1. 向您添加一个属性(例如 self.flipSideController)以存储对;MainViewController的引用 FlipsideViewController然后调用SaveFPQData它(例如。[self.flipSideController SaveFPQData];或

  2. 使用通知中心发布来自saveButton:该触发器的通知SaveFPQData;这会是这样的:

    //-- in flipsidecontroller `viewDidLoad`:
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(SaveFPQData)
                                             name:@"SaveFPQData"
                                           object:nil];
    
    //-- in saveButton:
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SaveFPQData" object:nil];
    

第二种方法实现起来最简单,IMO,它允许最松散的耦合,但会牺牲一些时钟周期。

编辑:

我并不完全清楚您要做什么(具体来说,我不完全了解如何在显示 FlipsideViewController 后按下 MainViewController 中的按钮;另一方面,如果您不继续使用 FlipsideViewController,那么它不存在,因此您无法向其发送消息),无论如何您可以尝试在以下位置初始化您的self.flipsideViewController属性prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([[segue identifier] isEqualToString:@"showAlternate"]) {
    UIViewController* controller = [segue destinationViewController];
    [controller setDelegate:self];
    if ([controller isKindOfClass:[FlipsideViewController class]])
         self.flipsideViewController = (id)controller;
  }
}

完成此操作后,您MainViewController将能够将saveFPQ消息发送到FlipsideViewController.

如果你的意思是你想在segue-ing 到 FlipsideViewController之前saveFPQ发送消息,你应该对它进行 segue 并调用方法。saveButtonsaveFPQ

我怀疑你需要某种可以从主视图和反面视图控制器访问的“模型”对象。

希望这可以帮助。

于 2013-02-16T21:16:41.820 回答