0

我想從另一個ViewController更改mapType,但它只顯示HybridType。任何其他mapType都不會改變我的分段控件上的按鈕被按下。我究竟做錯了什麼?預先感謝您..NSNotificationCenter和mapType

-(void) receiveTestNotification:(NSNotification *) notification{ 
_mapView.delegate = self; 
if ([[notification name] isEqualToString:@"TestNotification"]) { 
    _mapView.mapType = MKMapTypeStandard; 
    NSLog(@"Successfully changed maptype"); 
} 
if ([[notification name] isEqualToString:@"TestNotification2"]) { 
    _mapView.mapType = MKMapTypeSatellite; 
    NSLog(@"Successfully changed maptype"); 
} 
if ([[notification name] isEqualToString:@"TestNotification3"]) { 
    _mapView.mapType = MKMapTypeHybrid; 
    NSLog(@"Successfully changed maptype"); 
} 

} 

在viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"TestNotification" object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"TestNotification2" object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"TestNotification3" object:nil]; 

,並在我的viewController其他:

- (IBAction)segmentedControl:(id)sender { 

switch (((UISegmentedControl *) sender).selectedSegmentIndex) { 
    case 0: 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self]; 
     [self dismissModalViewControllerAnimated:YES]; 
    case 1: 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification2" object:self]; 
     [self dismissModalViewControllerAnimated:YES]; 
    case 2: 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification3" object:self]; 
     [self dismissModalViewControllerAnimated:YES]; 
} 
} 

回答

1

Everythiing似乎罰款。檢查分段控制中的所有iboutlet,並且在這種情況下日誌可能會更有幫助。另一件事你應該使用break;語句,否則它繼續執行下面的代碼,所以當你想發送TestNotification時,它也會在下面發送其他兩個通知。

1

你錯過了「break」在switch/case。