如何检测按下的button,并显示在另一个视图? Objective-C的

我是iOS应用开发新手。 我想在iOS中创build一个具有分割视图的计算器应用程序。 左侧是滚动视图中的“历史”function,右侧是计算器本身。 现在,关于这个应用程序的历史function,我认为我的程序需要识别什么被按下,并按下等于(=)button时显示在滚动视图。 你有什么想法这将继续Objective-C吗? 我正在使用XCode 4.5和iPhone模拟器6.0。

提前致谢!

如果你想在视图或视图控制器之间进行通信/发送数据,有几个选项。

如果您尝试在视图之间传递/发送数据,并且您可以参考两个视图,则可以简单地从您的视图中调用方法

LeftView.h

@interface LeftView : UIView { //instance variables here } //properties here //other methods here -(NSInteger)giveMeTheValuePlease; @end 

LeftView.m

 @implementation LeftView //synthesise properties here //other methods implementation here -(NSInteger)giveMeTheValuePlease { return aValueThatIsInteger; //you can do other computation here } 

RightView.h

  @interface RightView : UIView { //instance variables here } //properties here //other methods here -(NSInteger) hereIsTheValue:(NSInteger)aValue; @end 

RightView.m

  @implementation LeftView //synthesise properties here //other methods implementation here -(void)hereIsTheValue:(NSInteger)aValue { //do whatever you want with the value } 

AViewController.m

 @implementation AViewController.m //these properties must be declared in AViewController.h @synthesise leftView; @synthesise rightView; -(void)someMethod { NSInteger aValue = [leftView giveMeTheValuePlease]; [rightView hereIsTheValue:rightView]; } 

您可以使用委托模式(在iOS中非常常见),这是一个简短而基本的委托代码示例,您可以在此链接中find我的答案之一

您也可以使用块来在视图/视图控制器之间进行通信/发送数据,但是这个主题我认为您稍后会用到,为了获得iOS块的基本概念,您将不得不谷歌一点点。

这是这个要求的解决scheme。

在我的情况..我有2个button在viewcontroller。 当我点击这些button,我不得不显示popover。 为此,我必须检测PopoverController(AnotherViewController)中的哪个button被单击。

首先,我采取了@property BOOL isClicked; 在AppDelegate.h中

而在AppDelegate.m @synthesize isClicked; (合成它)和中

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. isClicked = FALSE; } 

现在在ViewController.m中,对于像这样改变的button,

 - (IBAction)citiesButtonClicked:(id)sender { AppDelegate *delegate = [UIApplication sharedApplication].delegate; delegate.isClicked = FALSE; } - (IBAction)categoryButtonClicked:(id)sender { AppDelegate *delegate = [UIApplication sharedApplication].delegate; delegate.isClicked = TRUE; } 

PopoverViewController(AnotherViewController)中的-(void)viewDidLoad方法

 -(void)viewDidLoad { { AppDelegate *delegate = [UIApplication sharedApplication].delegate; if (delegate.isClicked) { delegate.isClicked = FALSE; NSLog(@"popover clicked"); } else { delegate.isClicked = TRUE; isClicked = YES; } } 

我希望它有帮助。 让我知道你是否需要任何帮助。

Interesting Posts