我對編寫基於VB的應用程序非常熟悉,但對Xcode(和Objective C)是新手。我已經瀏覽了網絡上的大量教程,並瞭解了基本知識以及如何與Interface Builder進行交互。但是,我真的很困惑於C語言的一些基本概念,並且非常感謝您提供的任何幫助。繼承人我的問題...如何從另一個類文件調用函數
我有一個簡單的iphone應用程序有一個視圖控制器(FirstViewController)和一個子視圖(SecondViewController)與相關的標題和類文件。
在FirstViewController.m有一個函數定義
@implementation FirstViewController
- (void) writeToServer:(const uint8_t *) buf {
[oStream write:buf maxLength:strlen((char*)buf)];
}
它並不真正重要的功能是什麼。
我想在我的SecondViewController使用此功能,所以在SecondViewController.m我進口FirstViewController.h
#import "SecondViewController.h"
#import "FirstViewController.h"
@implementation SecondViewController
-(IBAction) SetButton: (id) sender {
NSString *s = [@"Fill:" stringByAppendingString: FillLevelValue.text];
NSString *strToSend = [s stringByAppendingString: @":"];
const uint8_t *str = (uint8_t *) [strToSend cStringUsingEncoding:NSASCIIStringEncoding];
FillLevelValue.text = strToSend;
[FirstViewController writeToServer:str];
}
最後一行是我的問題。 XCode告訴我FirstViewController可能不會響應writeToServer。 當我嘗試運行該應用程序時,它調用此函數時崩潰。
我想我並不完全理解如何共享函數,更重要的是類之間的關係。
在理想的世界裏,我會創建一個全球化的課程,把我的功能放入並根據需要調用它們。
任何建議感激地收到。
您更好地利用委託 – 2015-05-10 03:28:01
我已經高達投URS希望把它找回來我的回答太 – 2015-06-20 14:11:17