I have created a simple TableView application using delegate and protocols, it worked in ios 5 but when i updated my xcode, i does not work in ios 6.
The code is like this:
In child view:
#class AddItemViewController;
#class CheckListItem;
#protocol AddItemViewControllerDelegate <NSObject>
- (void)addItemViewControllerDidCancel: (AddItemViewController *)controller;
- (void)addItemViewController:(AddItemViewController *)controller didFnishAddingItem:(CheckListItem *)item;
#end
#interface AddItemViewController : UITableViewController <UITextFieldDelegate>
//declare an property
#property (nonatomic, assign) id <AddItemViewControllerDelegate> delegate;
in childview.m:
- (void)addItemViewControllerDidCancel: (AddItemViewController *)controller{
//i do something here
}
in parentview.h:
#import "AddItemViewController.h"
#interface CheckListViewController : UITableViewController <AddItemViewControllerDelegate>
-(IBAction)addItem;
#end
in parentview.m:
- (void)addItemViewControllerDidCancel:(AddItemViewController *)controller
{
[controller dismissViewControllerAnimated:YES completion:nil];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"AddItem"]){
UINavigationController *navigationController = segue.destinationViewController;
AddItemViewController *controller = (AddItemViewController *)navigationController;
controller.delegate = self;
NSLog(#"perform prepare for segue");
}
}
Is there anything wrong with this code?
Thanks
In your child view you have to call [delegate addItemViewControllerDidCancel: (AddItemViewController *)controller], instead of just calling the method.
If you change that, it should work :)
Related
I have a text view on the main view controller. I have a bar button item on the view controller's navigation bar. When the app starts, I perform the following actions:
Tap the text view to begin editing and to show the keyboard.
Tap the bar button to show a popover view.
Without dismissing the popover view, I dismiss the keyboard.
Dismiss the popover view by tapping any other view on the screen.
Before iOS 11, the keyboard will NOT show up again after Step 4. However, in iOS 11, it will show up. It seems that in iOS 11, it restores the first responder after dismissing the popover view.
Here are my questions:
Is it a bug, or some changes in iOS 11?
If it is new, then how can I prevent the keyboard from showing after dismissing the popover view?
Also see the following videos:
For iOS 11:
https://www.dropbox.com/s/88wyv0y0idsmu5c/iOS%2011.mov?dl=0
For iOS 10.3:
https://www.dropbox.com/s/11gg6h39mcgb0fs/iOS%2010.3.mov?dl=0
Here are some codes:
#import "MainViewController.h"
#interface MainViewController ()
#property(nonatomic, retain)UITextView *textView;
#end
#implementation MainViewController
#synthesize textView = _textView;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.textView = [[UITextView alloc] init];
self.textView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.textView];
self.textView.backgroundColor = [UIColor blueColor];
NSDictionary *dict = #{#"textView" : self.textView};
NSArray *vCons = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-3-[textView]-3-|" options:0 metrics:nil views:dict];
NSArray *hCons = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-3-[textView]-3-|" options:0 metrics:nil views:dict];
[self.view addConstraints:vCons];
[self.view addConstraints:hCons];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
#import "ViewController.h"
#import "MainViewController.h"
#interface ViewController ()
#property(retain,nonatomic)MainViewController *mainVC;
#end
#implementation ViewController
#synthesize mainVC = _mainVC;
- (void)viewDidLoad {
[super viewDidLoad];
self.mainVC = [[MainViewController alloc] init];
UINavigationController *navigCon = [[UINavigationController alloc] initWithRootViewController:self.mainVC];
self.mainVC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Button" style:UIBarButtonItemStylePlain target:self action:#selector(showPopover)];
[self.view addSubview:navigCon.view];
}
-(void)showPopover {
UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:#"Action 1" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:#"Action 2" style:UIAlertActionStyleDefault handler:nil];
[alertCon addAction:action1];
[alertCon addAction:action2];
[alertCon setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alertCon popoverPresentationController];
popPresenter.barButtonItem = self.mainVC.navigationItem.rightBarButtonItem;
[self presentViewController:alertCon animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
I just had this issue on iOS 11 with a swift app. Calling [textView resignFirstResponder] any time before dismissing the new controller was the only fix. [view endEditing] was not enough.
I read ALL questions with this tittle. FavoritosViewController.m I have:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
favcolCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"fav" forIndexPath:indexPath];
[[cell imageCell]setImage:[UIImage imageNamed:[arrayimages objectAtIndex:indexPath.item]]];
return cell;
}
on favcolCell.h I have:
#property (retain, nonatomic) IBOutlet UIImageView *imageCell;
on favcolCell.m I have:
#synthesize imageCell;
(...)
- (void)dealloc {
[imageCell release];
[super dealloc];
}
What I'm missing?
EDIT:
Solution: Do the right class registration.
I missed this:
[self.collectionView registerClass:[FavoritosRestViewCell class] forCellWithReuseIdentifier:#"fav"];
on the ViewDidLoad Method. I registered the wrong class. Thanks to user HotLicks.
For me this is the solutions..
I have taken one collection view and imageView inside cell. But I forgot to set tag in the interface(storyboard) file.
Storyboard-> collection view->image view->property inspector->set tag here.
I hope it helps someone.
I'm using UISearchDisplayControlleron UInavigationBar but the background color of UISearchDisplayController is not matching the color of uinavigationbar. i used the following code in ma ViewDidLoad but no change I'm unable to change the color of UISearchDisplayController
self.searchDisplayController.searchBar.tintColor = [UIColor clearColor];
A UISearchDisplayController is not an interface element. It is a controller, not a view. It has no color.
What I did was I created a UISearchBar subclass and in IB made the searchBar belonging to the UISearchDisplayController part of that subclass. From there I was able to change the tintColor and backgroundColor. The reason I needed this was because the UISearchBar was not displaying correctly within a UINavigationBar and this was the only way I could think of on, how to do it.
Edit
Your actually able to do this relatively easy with UISearchDisplayController's initializer, initWithSearchBar:contentsController:, however you'll still need to subclass UISearchBar.
Here is the Code I used. Now if your performing this through IB you only need to use initWithCoder: but I created a setup method. Just incase, I need to use it again, in places other than IB.
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
-(id) initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setup];
}
return self;
}
-(id)init {
if (self = [super init]) {
[self setup];
}
return self;
}
-(void) setup {
self.backgroundColor = [UIColor clearColor];
self.tintColor = [UIColor clearColor];
for (UIView * view in self.subviews) {
if ([view isMemberOfClass:NSClassFromString(#"UISearchBarBackground")]) {
view.alpha = 0.0;
}
}
}
The following code worked fine is IOS 5, but now handleTapGesture doesn't even get called in IOS 6. What changed?
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[self addGestureRecognizer:tap];
}
return self;
}
- (void)handleTapGesture:(UITapGestureRecognizer *)recognizer
{
MessageNib *cell = self;
MessageView *view = (MessageView *)[[[NSBundle mainBundle] loadNibNamed:#"MessageView" owner:self options:nil] objectAtIndex:0];
view.message = cell.message;
[[NSNotificationCenter defaultCenter] postNotificationName:NEW_SUB_PAGE object:view];
}
I guess you have to set the numberOfTaps after alloc init
tap.numberOfTapsRequired = 1;
Or the other possibility is initWithCoder was not called.
Ended up just putting a "phantom" button (aka button with no content) over my item I wanted tappable and attached a Touch Up Inside event to the button which calls my tap gesture code.
This is a decent momentary hack anyway.
I have a table with a big list of stuff that comes from a plist file and clicking each of them takes you to a new view, a xib.
I have 2 views inside that .xib, one for portrait and one for landscape
In my h file I have this:
IBOutlet UIView *portraitView;
IBOutlet UIView *landscapeView;
#property (nonatomic, retain) IBOutlet UIView *portraitView;
#property (nonatomic, retain) IBOutlet UIView *landscapeView;
In my m file this:
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) orientationChanged:(id)object
{
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portraitView;
}
else
{
self.view = self.landscapeView;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
self.view = portraitView;
} else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
self.view = landscapeView;
}
return YES;
}
#end
Everything was working perfectly in iOS 5, showing landscape or portrait when needed.
Now with the iOS 6 update everything is a mess.
If I am in the table (portrait) view and click one item, it shows correct in portrait, if I rotate to landscape, the view shows the correct view as well, BUT being in landscape, if I go back to the table and select another item, it shows the portrait view instead of the landscape.
If I do the same but starting landscape, it shows portrait.
So, now the orientation is not working for anything.
The same happens to my other views using storyboard. They are portrait and always showed like that, now they rotate, shrink everything and leave my app as trash.
1- How can I fix the .xib orientation thing ?
2- How can I fix the storyboard orientation ? (they were static, now everything rotates (no code at all))
Thanks.
I think that I have a work around. It's ugly but it's working...
With iOS6 Apple suggest now to use 2 differences XIB file to switch between portrait & landscape view.
But if you want to use the previous method allowed in iOS 5.0 by "switching" between 2 UIView IBOutlet, you can try my ugly working solution. The idea is to rotate the view according to the orientation.
1) In you viewDidLoad, subscribe to orientation notification:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
2) Add a method called by the notification:
-(void)orientationChanged:(NSNotification *)object{
NSLog(#"orientation change");
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation == UIInterfaceOrientationPortraitUpsideDown){
self.view = self.potraitView;
if(deviceOrientation ==UIInterfaceOrientationPortraitUpsideDown){
NSLog(#"Changed Orientation To PortraitUpsideDown");
[self portraitUpsideDownOrientation];
}else{
NSLog(#"Changed Orientation To Portrait");
[self portraitOrientation];
}
}else{
self.view = self.landscapeView;
if(deviceOrientation ==UIInterfaceOrientationLandscapeLeft){
NSLog(#"Changed Orientation To Landscape left");
[self landscapeLeftOrientation];
}else{
NSLog(#"Changed Orientation To Landscape right");
[self landscapeRightOrientation];
}
}
}
3) And finally, add rotation method for each orientation:
-(void)landscapeLeftOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(-(3.14159/2));
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 480, 320);
self.view.bounds = contentRect;
}
-(void)landscapeRightOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 480, 320);
self.view.bounds = contentRect;
}
-(void)portraitOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(0);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 320, 480);
self.view.bounds = contentRect;
}
-(void)portraitUpsideDownOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 320, 480);
self.view.bounds = contentRect;
}
I suggest you to make a custom UIViewController class and inherit-ate from this class to save redundant code.
If you want to support both solution for ios5 and ios6 you can use a #endif macro to include the both code in your controllers.
Cheers
No need to send and receive notifications:
In your appdelegate.m the following method
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
is always called to check the window's orientation,
so a simple way around is to have the below described code in your appdelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if(self.window.rootViewController){
UIViewController *presentedViewController ;
if ([self.window.rootViewController isKindOfClass:([UINavigationController class])])
{
presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
}
else if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]){
UITabBarController *controller = (UITabBarController*)self.window.rootViewController;
id selectedController = [controller presentedViewController];
if (!selectedController) {
selectedController = [controller selectedViewController];
}
if ([selectedController isKindOfClass:([UINavigationController class])])
{
presentedViewController = [[(UINavigationController *)selectedController viewControllers] lastObject];
}
else{
presentedViewController = selectedController;
}
}
else
{
presentedViewController = self.window.rootViewController;
}
if ([presentedViewController respondsToSelector:#selector(supportedInterfaceOrientations)]) {
orientations = [presentedViewController supportedInterfaceOrientations];
}
}
return orientations;
}
and implement
- (NSUInteger)supportedInterfaceOrientations
in the respective view controllers
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait; //Or anyother orientation of your choice
}
and to perform sudden action against orientation changes, implement the following method
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
This is very late for an answer, still I thought I should share this with you just in case,
I had the very same issue.
shouldAutorotateToInterfaceOrientation is deprecated iOS 6 onwards.
You need to parallel this method with the new supportedInterfaceOrientations and shouldAutorotate methods.
And it is very very important, you need to make sure that you set the root controller in your app delegate's applicationDidFinishLaunching method rather than simply adding the view controller's view ( or navigation Controller or tabBar Controller depending on what you are using ) as a subview to the window.