- UIKit is the old way of developing iOS apps
- SwfitUI (which we have learned in this course) replaced it and is doing most of the things
- There are still some things that might be better in UIKit or there might be some old code so it's important to understand how to use it in SwiftUI
- Instead of having MVVM there is MVC
- In MVC views are grouped together and controlled by a
Controller
- In MVC views are grouped together and controlled by a
- UIKit is object-oriented (not functional)
- UIKit uses a concept "delegation"
- Objects (controllers and views) often delegate some of their functionality to other objects
- They do this by having a var called
delegate
delegate
var is constrained via a protocol with all the delegatable functionality
Enroute demo UIView (12:25)
- We create
MapView
to normal Swift file (not SwiftUI) because there isn'tvar body
This will draw a map and add pins to the points mentioned inannotations
importSwfitUIimportUIKitimportMapKitstructMapView:UIViewRepresentable{letannotations:[MKAnnotation]funcmakeUIView(context:Context)->MKMapView{letmkMapView=MKMapView()mkMapView.delegate=context.coordinatormkMapView.addAnnotations(self.annotations)returnmkMapView}funcupdateUIView(_uiView:MKMapView,context:Context){}funcmakeCoordinator()->Coordinator{returnCoordinator()}classCoordinator:NSObject,MKMapViewDelegate{funcmapView(_mapView:MKMapView,viewForannotation:MKAnnotation)->MKAnnotationView?{letview=mapView.dequeueReusableAnnotationView(withIdentifier:"MapViewAnnotation")??MKPinAnnotationView(annotation:annotation,reuseIdentifier:"MapViewAnnotation")view.canShowCallout=truereturnview}}}
EmojiArt demo UIViewController (39:13)
importSwiftUIimportUIKittypealiasPickedImageHandler=(UIImage?)->VoidstructImagePicker:UIViewControllerRepresentable{// could be handled using binding but wanted to show// that there are different waysvarhandlePickedImage:PickedImageHandlerfuncmakeUIViewController(context:Context)->UIImagePickerController{letpicker=UIImagePickerController()picker.sourceType=.photoLibrarypicker.delegate=context.coordinatorreturnpicker}funcupdateUIViewController(_uiViewController:UIImagePickerController,context:Context){}funcmakeCoordinator()->Coordinator{Coordinator(handlePickedImage:handlePickedImage)}classCoordinator:NSObject,UIImagePickerControllerDelegate,UINavigationControllerDelegate{varhandlePickedImage:PickedImageHandlerinit(handlePickedImage:@escapingPickedImageHandler){self.handlePickedImage=handlePickedImage}funcimagePickerController(_picker:UIImagePickerController,didFinishPickingMediaWithInfoinfo:[UIImagePickerController.InfoKey:Any]){handlePickedImage(info[.originalImage]as?UIImage)}funcImagePickerControllerDidCancel(_picker:UIImagePickerController){handlePickedImage(nil)}}}
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse