Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Lankinen
Lankinen

Posted on

     

CS193p Notes - Lecture 14: UIKit Integration

  • 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 aController
  • UIKit is object-oriented (not functional)

Alt Text

Alt Text

  • 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 calleddelegate
    • delegate var is constrained via a protocol with all the delegatable functionality

Enroute demo UIView (12:25)

  • We createMapView 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)}}}

@RealLankinen

Originally published here

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Not ready...
  • Location
    Earth
  • Joined

More fromLankinen

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp