我需要存储整数和一些花车来分享不同的视图,我知道在AppDelegate中这样做是假的,但这是我正在进行的入门iOS课程,我们还没有得到很高级,我意味着AT ALL(没有MVC范例,没有Objective-C课程,没有核心数据,没有我们可能遇到的错误或如何处理它们等)。我的应用程序是针对餐馆中的服务器,我有6个视图作为子视图加载来自应用程序的ViewController,其中包含不同的项目,使用“+”和“ - ”按钮来增加或减少订购数量。在AppDelegate中,我有int
个和float
的每个项目数量和我声明为属性的小计。
项目的每个数量都会更改,它会调用更新项目计数器的方法,显示数量,并更新并显示该视图的小计。在那个方法中我也是 创建委托的实例(?),以便我可以访问我声明的int和float属性。
-(void)updateAppSubtotal{
NSString *preSubString;
NSString *subString;
Final2AppDelegate* delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];
delegate.eggTotal = eggs;
delegate.craTotal = crabs;
delegate.capTotal = caps;
delegate.quiTotal = quis;
delegate.bruTotal = brus;
delegate.pepTotal = peps;
delegate.tarTotal = tars;
delegate.balTotal = bals;
delegate.appSubtotal = appSub;
appSub = (eggs*eggPrice)+(crabs*crabPrice)+(caps*capPrice)+(quis*quiPrice)+(brus*bruPrice)+
(peps*pepPrice)+(tars*tarPrice)+(bals*balPrice);
if(appSub < 10){
preSubString = [[NSString alloc]initWithFormat:@"%1.2f",appSub];
}else if(appSub < 100 && appSub >= 10){
preSubString = [[NSString alloc]initWithFormat:@"%2.2f",appSub];
}else if(appSub < 1000 && appSub >= 100){
preSubString = [[NSString alloc]initWithFormat:@"%3.2f",appSub];
}else{
preSubString = [[NSString alloc]initWithFormat:@"%4.2f",appSub];
}
subString = [[NSString alloc]initWithFormat:@"$%@",preSubString];
appSubtotal.text = subString;
[preSubString release];
[subString release];
}
然后在“账单”的视图中,我使用它来(尝试)用数量和小计填充标签:
- (void)viewDidLoad {
NSString *preSubString;
NSString *subString;
NSString *preTaxString;
NSString *taxString;
NSString *preTotString;
NSString *totString;
Final2AppDelegate *delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];
subFloat = delegate.appSubtotal + delegate.bevSubtotal + delegate.entSubtotal + delegate.sidSubtotal +
delegate.desSubtotal;
if(subFloat < 10){
preSubString = [[NSString alloc]initWithFormat:@"%1.2f",subFloat];
}else if(subFloat < 100 && subFloat >= 10){
preSubString = [[NSString alloc]initWithFormat:@"%2.2f",subFloat];
}else if(subFloat < 1000 && subFloat >= 100){
preSubString = [[NSString alloc]initWithFormat:@"%3.2f",subFloat];
}else{
preSubString = [[NSString alloc]initWithFormat:@"%4.2f",subFloat];
}
subString = [[NSString alloc]initWithFormat:@"$%@",preSubString];
billSubtotal.text = subString;
taxFloat = subFloat*taxRate;
if(taxFloat < 10){
preTaxString = [[NSString alloc]initWithFormat:@"%1.2f",taxFloat];
}else if(taxFloat < 100 && taxFloat >= 10){
preTaxString = [[NSString alloc]initWithFormat:@"%2.2f",taxFloat];
}else if(taxFloat < 1000 && taxFloat >= 100){
preTaxString = [[NSString alloc]initWithFormat:@"%3.2f",taxFloat];
}else{
preTaxString = [[NSString alloc]initWithFormat:@"%4.2f",taxFloat];
}
taxString = [[NSString alloc ]initWithFormat:@"$%@",preTaxString];
tax.text = taxString;
totalFloat = subFloat + taxFloat;
if(totalFloat < 10){
preTotString = [[NSString alloc]initWithFormat:@"%1.2f",totalFloat];
}else if(totalFloat < 100 && totalFloat >= 10){
preTotString = [[NSString alloc]initWithFormat:@"%2.2f",totalFloat];
}else if(totalFloat < 1000 && totalFloat >= 100){
preTotString = [[NSString alloc]initWithFormat:@"%3.2f",totalFloat];
}else{
preTotString = [[NSString alloc]initWithFormat:@"%4.2f",totalFloat];
}
totString = [[NSString alloc]initWithFormat:@"$%@",preTotString];
total.text = totString;
eggFinalQty.text = [NSString stringWithFormat:@"%d",delegate.eggTotal];
crabFinalQty.text = [NSString stringWithFormat:@"%d",delegate.craTotal];
capFinalQty.text = [NSString stringWithFormat:@"%d",delegate.capTotal];
quiFinalQty.text = [NSString stringWithFormat:@"%d",delegate.quiTotal];
bruFinalQty.text = [NSString stringWithFormat:@"%d",delegate.bruTotal];
pepFinalQty.text = [NSString stringWithFormat:@"%d",delegate.pepTotal];
tarFinalQty.text = [NSString stringWithFormat:@"%d",delegate.tarTotal];
balFinalQty.text = [NSString stringWithFormat:@"%d",delegate.balTotal];
oldFinalQty.text = [NSString stringWithFormat:@"%d",delegate.oldTotal];
vodkaFinalQty.text = [NSString stringWithFormat:@"%d",delegate.vodTotal];
martiniFinalQty.text = [NSString stringWithFormat:@"%d",delegate.martTotal];
redFinalQty.text = [NSString stringWithFormat:@"%d",delegate.redTotal];
whiteFinalQty.text = [NSString stringWithFormat:@"%d",delegate.whiTotal];
ipaFinalQty.text = [NSString stringWithFormat:@"%d",delegate.ipaTotal];
oliFinalQty.text = [NSString stringWithFormat:@"%d",delegate.oldTotal];
ribFinalQty.text = [NSString stringWithFormat:@"%d",delegate.ribTotal];
ravFinalQty.text = [NSString stringWithFormat:@"%d",delegate.ravTotal];
coqFinalQty.text = [NSString stringWithFormat:@"%d",delegate.coqTotal];
bufFinalQty.text = [NSString stringWithFormat:@"%d",delegate.bufTotal];
walFinalQty.text = [NSString stringWithFormat:@"%d",delegate.walTotal];
cubFinalQty.text = [NSString stringWithFormat:@"%d",delegate.cubTotal];
bleFinalQty.text = [NSString stringWithFormat:@"%d",delegate.bleTotal];
sauFinalQty.text = [NSString stringWithFormat:@"%d",delegate.sauTotal];
cacFinalQty.text = [NSString stringWithFormat:@"%d",delegate.cacTotal];
souFinalQty.text = [NSString stringWithFormat:@"%d",delegate.souTotal];
pesFinalQty.text = [NSString stringWithFormat:@"%d",delegate.pesTotal];
temFinalQty.text = [NSString stringWithFormat:@"%d",delegate.temTotal];
salFinalQty.text = [NSString stringWithFormat:@"%d",delegate.salTotal];
lobFinalQty.text = [NSString stringWithFormat:@"%d",delegate.lobTotal];
dreFianlQty.text = [NSString stringWithFormat:@"%d",delegate.dreTotal];
marFinalQty.text = [NSString stringWithFormat:@"%d",delegate.marTotal];
cheFinalQty.text = [NSString stringWithFormat:@"%d",delegate.cheTotal];
fonFinalQty.text = [NSString stringWithFormat:@"%d",delegate.fonTotal];
mouFinalQty.text = [NSString stringWithFormat:@"%d",delegate.mouTotal];
pieFinalQty.text = [NSString stringWithFormat:@"%d",delegate.pieTotal];
hazFinalQty.text = [NSString stringWithFormat:@"%d",delegate.hazTotal];
briFinalQty.text = [NSString stringWithFormat:@"%d",delegate.briTotal];
choFinalQty.text = [NSString stringWithFormat:@"%d",delegate.choTotal];
iceFinalQty.text = [NSString stringWithFormat:@"%d",delegate.iceTotal];
[preSubString release];
[subString release];
[preTaxString release];
[taxString release];
[preTotString release];
[totString release];
[super viewDidLoad];
}
但是当视图加载每个项目的所有数量标签时,税收,小计和总计的标签都是0.这些值似乎没有更新,我不知道为什么。我曾和我的一位朋友谈过上学期的这门课(我正在做一个直接的学习,所以非常感谢StackOverflow上的所有人都是我的信息和想法资源)而且我做的几乎一样他做的事。
为什么delegate
中的值未更新?
F.Y.I。我对每个视图都有类似的更新方法,每个方法都使用Final2AppDelegate *delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];
。这是否与它有关,因为委托的多个实例(?)?
我曾尝试使用viewWillAppear
和viewDidAppear
方法,但没有这样的运气。我无法使用常规字符串更新标签。我确保所有的连接都已完成,但仍然无法正常工作。我正在考虑重新创建视图,因为我必须在几天前重做其他视图以确定,但我不知道这会如何影响这一点。
所以我在饮料视图上制作了标签,更新了delegate.foo
的价值,看起来他们正确地更新了,我使用了同样的声明,我一直试图在'比尔的观点显示计数,也正常工作。中间有什么东西正在发生吗?我将尝试在开胃菜视图上添加一个新标签,该标签会更新其中一个饮料,以查看是否会在视图之间共享数据。
那也不起作用(我不知道为什么会这样)所以显然在尝试从不同视图访问数据时遇到了一些麻烦。因为每次我都看不到这会产生什么影响,所以我的所有ViewControllers都只是指向同一个实例。我可以访问我创建它们的视图中的值,但是如果我尝试从其他视图中获取0,那么任何人都有更多的洞察力来阐明这一点吗?
可能会使用应用程序的ViewController,所有其他视图都是子视图而不是AppDelegate有所作为吗?
每次调用更新方法时,我都会创建一个指向AppDelegate的新指针,这是每次按下加号或减号按钮时。这会对它产生影响吗?
这就是我控制视图的方式。我当时认为viewDid/WillAppear
和viewDid/WillDisappear
可能不会被调用,因为它是如何被控制的,但这超出了我的范围。我知道代码比它必须的长,但在这一点上,我只是想完成这个项目并完成这个定向研究! :d
-(IBAction)loadBeverageView:(id)sender{
[self clearView];
[self.view insertSubview:beverageViewController.view atIndex:0];
}
-(IBAction)loadAppetizerView:(id)sender{
[self clearView];
[self.view insertSubview:appetizerViewController.view atIndex:0];
}
-(IBAction)loadEntreeView:(id)sender{
[self clearView];
[self.view insertSubview:entreeViewController.view atIndex:0];
}
-(IBAction)loadSideView:(id)sender{
[self clearView];
[self.view insertSubview:sideViewController.view atIndex:0];
}
-(IBAction)loadDessertView:(id)sender{
[self clearView];
[self.view insertSubview:dessertViewController.view atIndex:0];
}
-(IBAction)loadBillView:(id)sender{
[self clearView];
[self.view insertSubview:billViewController.view atIndex:0];
}
-(void)clearView{
if(beverageViewController.view.superview){
[beverageViewController.view removeFromSuperview];
}else if(appetizerViewController.view.superview){
[appetizerViewController.view removeFromSuperview];
}else if(entreeViewController.view.superview){
[entreeViewController.view removeFromSuperview];
}else if(sideViewController.view.superview){
[sideViewController.view removeFromSuperview];
}else if(dessertViewController.view.superview){
[dessertViewController.view removeFromSuperview];
}else{
[billViewController.view removeFromSuperview];
}
}
答案 0 :(得分:2)
您的问题可能是您尝试在viewDidLoad中更新帐单视图。如果您已经加载了viewController或将其添加到选项卡栏控制器或其他任何内容,那么viewDidLoad将不再被调用 - 当视图控制器已完成从笔尖(或手动)加载其视图时,仅调用一次。
要在另一个控制器显示视图时更新视图,viewWillAppear是要使用的标准方法,但如果您按照问题的方式交换视图,则不会调用此方法。如果您的目标只是让这场噩梦过去,您可以手动调用viewWillAppear(不确定是否有任何副作用),或者将更新逻辑放入新方法中,并在每次呈现您的一个视图时调用它。
您可以通过添加日志语句或以各种方法设置断点来自行查看。这将告诉您何时实际执行各种方法。
此外,您对app委托的调用很好 - 您不是在创建新实例,而是每次只获取指向同一实例的指针。
我希望你在本课程上只浪费了时间,而不是任何钱!