从另一个角度看呼叫方法,我想?

时间:2013-02-16 20:50:31

标签: objective-c xcode

我有一个简单的实用程序应用程序,带有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
{

}

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

MainViewController.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

MainViewController.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

1 个答案:

答案 0 :(得分:1)

您主要有两种方式:

  1. MainViewController添加一个属性(例如self.flipSideController)以存储对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];
    
  3. 第二种方法是最简单的实现,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

    如果您的意思是希望在之前发送saveFPQ消息到FlipsideViewController,那么您应该saveButton转发它并调用{ {1}}方法。

    我怀疑你需要从主视图和翻转视图控制器访问某种“模型”对象。

    希望这有帮助。